Children 집계

Children 집계

children 집계는 인덱스에 정의된 부모-자식 관계를 기반으로 자식 문서를 담은 단일 버킷을 만드는 버킷 집계예요.

children 집계는 join 필드 타입과 함께 동작해서, 부모 문서에 연결된 자식 문서를 집계해요. children 집계는 특정 자식 관계 이름과 일치하는 자식 문서를 식별하는 반면, parent 집계는 일치하는 자식 문서가 있는 부모 문서를 식별해요. 두 집계 모두 자식 관계 이름을 입력으로 받아요.

출처: 문서

본문

파라미터 (Parameters)

children 집계는 다음 파라미터를 받아요.

파라미터 필수/선택 데이터 타입 설명
type 필수 String join 필드에서 가져온 자식 타입의 이름. 사용할 부모-자식 관계를 식별해요.

예제 (Example)

다음 예제는 세 명의 직원이 있는 작은 회사 데이터베이스를 만든다. 직원 레코드는 각각 부모 부서 레코드와 자식 join 관계를 가져요.

먼저 부서(부모)를 직원(자식)에 연결하는 join 필드가 있는 company 인덱스를 만들어요:

PUT /company
{
  "mappings": {
    "properties": {
      "join_field": {
        "type": "join",
        "relations": {
          "department": "employee"
        }
      },
      "department_name": {
        "type": "keyword"
      },
      "employee_name": {
        "type": "keyword"
      },
      "salary": {
        "type": "double"
      },
      "hire_date": {
        "type": "date"
      }
    }
  }
}

다음으로, 세 개의 부서와 세 명의 직원으로 데이터를 채워요. 부모-자식 지정은 다음 표에 나와 있어요.

부서 (부모) 직원 (자식)
Accounting Abel Anderson, Betty Billings
Engineering Carl Carter
HR 없음

routing 파라미터는 부모 문서와 자식 문서가 모두 같은 샤드에 저장되도록 보장해요. OpenSearch에서 부모-자식 관계가 올바르게 동작하려면 이 조건이 필요해요:

POST _bulk?routing=1
{ "create": { "_index": "company", "_id": "1" } }
{ "type": "department", "department_name": "Accounting", "join_field": "department" }
{ "create": { "_index": "company", "_id": "2" } }
{ "type": "department", "department_name": "Engineering", "join_field": "department" }
{ "create": { "_index": "company", "_id": "3" } }
{ "type": "department", "department_name": "HR", "join_field": "department" }
{ "create": { "_index": "company", "_id": "4" } }
{ "type": "employee", "employee_name": "Abel Anderson", "salary": 120000, "hire_date": "2024-04-04", "join_field": { "name": "employee",  "parent": "1" } }
{ "create": { "_index": "company", "_id": "5" } }
{ "type": "employee", "employee_name": "Betty Billings", "salary": 140000, "hire_date": "2023-05-05", "join_field": { "name": "employee",  "parent": "1" } }
{ "create": { "_index": "company", "_id": "6" } }
{ "type": "employee", "employee_name": "Carl Carter", "salary": 140000, "hire_date": "2020-06-06",  "join_field": { "name": "employee",  "parent": "2" } }

다음 요청은 모든 부서를 쿼리한 다음 Accounting이라는 이름의 부서로 필터링해요. 그런 다음 children 집계를 사용해 Accounting 부서와 자식 관계가 있는 두 문서를 선택해요. 마지막으로 avg 하위 집계가 Accounting 직원들의 평균 급여를 반환해요:

GET /company/_search
{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "join_field": "department"
          }
        },
        {
          "term": {
            "department_name": "Accounting"
          }
        }
      ]
    }
  },
  "aggs": {
    "acc_employees": {
      "children": {
        "type": "employee"
      },
      "aggs": {
        "avg_salary": {
          "avg": {
            "field": "salary"
          }
        }
      }
    }
  }
}

예제 응답 (Example response)

응답은 선택된 부서 버킷을 반환하고, 부서의 employee 타입 자식을 찾아 급여의 평균을 계산해요:

{
  "took": 379,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "acc_employees": {
      "doc_count": 2,
      "avg_salary": {
        "value": 110000
      }
    }
  }
}

더 알아보기 (Learn more)