Parent 집계

Parent 집계

parent 집계는 인덱스에 정의된 부모-자식 관계를 기반으로 부모 문서를 담은 단일 버킷을 만드는 버킷 집계예요. 이 집계를 사용하면 일치하는 자식 문서가 같은 부모 문서에 대해 분석을 수행할 수 있어 강력한 계층 데이터 분석이 가능해요.

parent 집계는 같은 인덱스의 문서 간 부모-자식 관계를 설정하는 join 필드 타입과 함께 동작해요.

parent 집계는 일치하는 자식 문서가 있는 부모 문서를 식별하는 반면, children 집계는 특정 자식 관계와 일치하는 자식 문서를 식별해요. 두 집계 모두 자식 관계 이름을 입력으로 받아요.

출처: 문서

본문

파라미터 (Parameters)

parent 집계는 다음 파라미터를 받아요:

파라미터 필수/선택 데이터 타입 설명
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" } }

마지막으로, 하나 이상의 직원과 부모 관계가 있는 모든 부서에 대한 집계를 실행해요:

GET /company/_search
{
  "size": 0,
  "aggs": {
    "all_departments": {
      "parent": {
        "type": "employee"
      },
      "aggs": {
        "departments": {
          "terms": {
            "field": "department_name"
          }
        }
      }
    }
  }
}

예제 응답 (Example response)

all_departments parent 집계는 employee 자식 문서가 있는 모든 부서를 반환해요. HR 부서는 표현되지 않는다는 점을 주목하세요:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 6,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "all_departments": {
      "doc_count": 2,
      "departments": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "Accounting",
            "doc_count": 1
          },
          {
            "key": "Engineering",
            "doc_count": 1
          }
        ]
      }
    }
  }
}

더 알아보기 (Learn more)