_count API와 agg count가 다름

개발새발·2023년 2월 12일
0

elasticsearch

목록 보기
45/54

이건 이전 글과 이어지는데, 이전 글의 문제를 해결하고 나니 agg의 count는 존재했지만 _count API 를 사용하여 나온 카운트와 값이 달랐다. 무슨 얘기인고 하니…

  1. ES agg 쿼리문
{
  "result_A": {
    "nested": {
      "path": "A"
    },
    "aggs": {
      "result_A_name": {
        "terms": {
          "size": 100,
          "field": "A.name"
        },
        "aggs": {
          "result_A_value": {
            "terms": {
              "size": 100,
              "field": "A.value"
            }
          }
        }
      }
    }
  }
}

⇒ 결과 : key a 의 doc_count = 24933

{
  "result_A": {
    "doc_count": 124946,
    "result_A_name": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "A.name",
          "doc_count": 102384,
          "product_prop_value": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "a",
                "doc_count": 24933
              },
              {
                "key": "a-1",
                "doc_count": 21049
              },
              {
                "key": "a-2",
                "doc_count": 7925
              },
							...
              {
                "key": "a-99",
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "A.name2",
          "doc_count": 22562,
          "product_prop_value": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "b",
                "doc_count": 24933
              },
              {
                "key": "b-1",
                "doc_count": 21049
              },
              {
                "key": "b-2",
                "doc_count": 7925
              },
							...
              {
                "key": "b-99",
                "doc_count": 1
              }
            ]
          }
        }
      ]
    }
  }
}
  1. count API 쿼리문
{
  "query": {
    "bool": {
      "must": [],
      "filter": {
        "bool": {
          "must": [
            {
              "nested": {
                "path": "A",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "term": {
                          "A.name": "A.name"
                        }
                      },
                      {
                        "terms": {
                          "A.value": [
                            "a"
                          ]
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

⇒ 결과 : a 의 doc_count = 24930

{
  "count": 126,
  "_shards": {
    "total": 24930,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  }
}

결론

결국 1번의 key a 와 2번의 a 의 조건이 분명히 같기 때문에, count가 같아야하는데.. 달랐다ㅠㅠ 원인을 찾아보니 문서중에 아래와 같은 데이터가 있었다.

{
  "A": [
    {
      "name": "A.name",
      "value": "a"
    },
    {
      "name": "A.name",
      "value": "a"
    },
    {
      "name": "A.name",
      "value": "a"
    }
  ]
}

이런 경우… _count API 는 중복되는 애를 빼고 카운트해주고, agg(집계)는 중복되는 애들까지 카운트를 해주고 있었다.

나는 일단 count가 다른 원인만 밝혀서 주면 되었기에 원인만 알고 끝냈고, 이 두 카운트를 똑같이 맞추는 법은 찾지 못했다 😓

profile
발새발개

0개의 댓글