# Elasticsearch top\_hits aggregation result and doc\_count are different

**URL:** <https://discuss.elastic.co/t/elasticsearch-top-hits-aggregation-result-and-doc-count-are-different/293400>\
**Category:** Elasticsearch\
**Created:** [January 4, 2022, 4:29am UTC](https://discuss.elastic.co/t/elasticsearch-top-hits-aggregation-result-and-doc-count-are-different/293400 "2022-01-04T04:29:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hyun](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hyun/32/97630_2.png) [@hyun](https://discuss.elastic.co/u/hyun)\
**Post date:** [January 4, 2022, 4:29am UTC](https://discuss.elastic.co/t/elasticsearch-top-hits-aggregation-result-and-doc-count-are-different/293400/1 "2022-01-04T04:29:19Z")

</div>

**Query**

```auto
GET /someindex/_search
{
   "size": 0,
   "query": {
      "ids": {
         "types": [],
         "values": ["08a2","08a3","03a2","03a3","84a1"]
      }
   },
   "aggregations": {
      "498": {
         "terms": {
            "field": "holderInfo.raw",
            "size": 50
         },
         "aggregations": {
            "tops": {
               "top_hits": {
                  "_source": {
                     "includes": ["uid"]
                  }
               }
            }
         }
      }
   }
}

```

**Result**

```auto
{
   ...
   "hits": {
      "total": 5,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "498": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "MATSUSHITA ELECTRIC INDUSTRIAL",
               "doc_count": 5,
               "tops": {
                  "hits": {
                     "total": 5,
                     "max_score": 1,
                     "hits": [
                        {
                           "_index": "someindex",
                           "_id": "03a3",
                           "_score": 1,
                           "_source": {
                              "uid": "03a3"
                           }
                        },
                        {
                           "_index": "someindex",
                           "_id": "08a2",
                           "_score": 1,
                           "_source": {
                              "uid": "08a2"
                           }
                        },
                        {
                           "_index": "someindex",
                           "_id": "84a1",
                           "_score": 1,
                           "_source": {
                              "uid": "84a1"
                           }
                        }
                     ]
                  }
               }
            }
         ]
      }
   }
}

```

"08a2", "08a3", "03a2", "03a3" and "84a1" each clearly have 'MATSUSHITA ELECTRIC INDUSTRIAL' in the holderInfo.raw field.

Therefore, there are 5 cases in the doc\_count, but only "03a3", "08a2", and "84a1" are output in the top\_hits results, and "08a3" and "03a2" are omitted.

**Query**

```auto
GET /someindex/_search
{
   "size": 0,
   "query": {
      "ids": {
         "types": [],
         "values": ["08a2","08a3","03a2","03a3","84a1"]
      }
   },
   "aggregations": {
      "498": {
         "terms": {
            "script": {
               "inline": "doc['holderInfo.raw'].value"
            },
            "size": 50
         }
      }
   }
}

```

**Result**

```auto
{
   ...
   "hits": {
      "total": 5,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "498": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "MATSUSHITA ELECTRIC INDUSTRIAL",
               "doc_count": 3
            }
         ]
      }
   }
}

```

In addition, two cases are omitted when aggregating with script.

I'd like to know why some uids are missing.

I'm in a situation where I have to use the Elasticsearch version 2.2. I want to know if it's an Elasticsearch bug that occurs in an old version or a user's fault.

Thanks!

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 23, 2022, 5:43pm UTC](https://discuss.elastic.co/t/elasticsearch-top-hits-aggregation-result-and-doc-count-are-different/293400/2 "2022-01-23T17:43:20Z")

</div>

> **[Top hits aggregation | Elasticsearch Guide \[7.16\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html)**

I'm not sure it is the same in 2.2 but top hits metric returns top three documents by default.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [January 23, 2022, 11:33pm UTC](https://discuss.elastic.co/t/elasticsearch-top-hits-aggregation-result-and-doc-count-are-different/293400/3 "2022-01-23T23:33:56Z")

</div>

> [@hyun](#):
>
> Elasticsearch version 2.2

This is very much past it's [EOL](https://www.elastic.co/support/eol) and definitely no longer supported. Unless there's a noticeable issue with your syntax that someone picks up that solves this, there's not a lot of chance of anyone being able to spot it as a bug.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 20, 2022, 11:34pm UTC](https://discuss.elastic.co/t/elasticsearch-top-hits-aggregation-result-and-doc-count-are-different/293400/4 "2022-02-20T23:34:03Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
