# Top hits query with same score?

**URL:** <https://discuss.elastic.co/t/top-hits-query-with-same-score/107018>\
**Category:** Elasticsearch\
**Created:** [November 9, 2017, 10:25am UTC](https://discuss.elastic.co/t/top-hits-query-with-same-score/107018 "2017-11-09T10:25:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![YuWatanabe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuwatanabe/32/13259_2.png) [@YuWatanabe](https://discuss.elastic.co/u/YuWatanabe)\
**Post date:** [November 9, 2017, 10:25am UTC](https://discuss.elastic.co/t/top-hits-query-with-same-score/107018/1 "2017-11-09T10:25:09Z")

</div>

I would like to ask a basic question about _top hits aggregation_ .

How does top hits aggregation choose the document when the score is the same?  
As condition , _sort order_ is set as _default_ and _size_ is set to _1_.

In below sample , top hits aggregation returned the earliest document.

```
[ywatanabe@localhost ~]$ curl -XGET localhost:9200/aggstest3/_search?pretty -d '{"size" : 2 , "query" : {"match_all" : {}}, "aggs" : { "host" : { "terms" : {"field" : "host.keyword" }, "aggs" : {"time" :{"top_hits" : { "size" : 1}} } }} }'
{
  "took" : 21,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "aggstest3",
        "_type" : "test",
        "_id" : "AV-gGu741A4C-ViaDhvV",
        "_score" : 1.0,
        "_source" : {
          "datetime" : "2017/11/09 00:00:00",
          "host" : "a"
        }
      },
      {
        "_index" : "aggstest3",
        "_type" : "test",
        "_id" : "AV-gGsQK1A4C-ViaDhuV",
        "_score" : 1.0,
        "_source" : {
          "datetime" : "2017/11/09 00:01:00",
          "host" : "a"
        }
      }
    ]
  },
  "aggregations" : {
    "host" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "a",
          "doc_count" : 2,
          "time" : {
            "hits" : {
              "total" : 2,
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "aggstest3",
                  "_type" : "test",
                  "_id" : "AV-gGu741A4C-ViaDhvV",
                  "_score" : 1.0,
                  "_source" : {
                    "datetime" : "2017/11/09 00:00:00",
                    "host" : "a"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [November 17, 2017, 8:56am UTC](https://discuss.elastic.co/t/top-hits-query-with-same-score/107018/2 "2017-11-17T08:56:48Z")

</div>

The top\_hits aggregation uses the internal doc\_id (in Lucene) as a tiebreak for documents with same sort values.  
The internal doc\_id can differ for the same document inside each replica of the same shard so it's recommended to use another tiebreaker for sort in order to get consistent results. Fo instance you could do:  
`sort: ["_score", "datetime"]` to force top\_hits to rank documents based on score first and use `datetime` as a tiebreaker.

---

<div class="post-metadata">

**Author:** ![YuWatanabe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuwatanabe/32/13259_2.png) [@YuWatanabe](https://discuss.elastic.co/u/YuWatanabe)\
**Post date:** [November 17, 2017, 10:04am UTC](https://discuss.elastic.co/t/top-hits-query-with-same-score/107018/3 "2017-11-17T10:04:05Z")

</div>

@jimczi

Thanks for the reply !

I would like to check one more thing. _doc\_id_ is not [\_id](https://www.elastic.co/guide/en/elasticsearch/guide/2.x/_document_metadata.html#_id) .

Correct?

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [November 17, 2017, 10:15am UTC](https://discuss.elastic.co/t/top-hits-query-with-same-score/107018/4 "2017-11-17T10:15:41Z")

</div>

Yes doc\_id is not the document \_id. This is an internal id that you cannot control and that is used by Lucene to identify documents inside an index.

---

<div class="post-metadata">

**Author:** ![YuWatanabe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuwatanabe/32/13259_2.png) [@YuWatanabe](https://discuss.elastic.co/u/YuWatanabe)\
**Post date:** [November 17, 2017, 10:28am UTC](https://discuss.elastic.co/t/top-hits-query-with-same-score/107018/5 "2017-11-17T10:28:53Z")

</div>

Got 8t. Thanks.

---

<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:** [December 15, 2017, 10:28am UTC](https://discuss.elastic.co/t/top-hits-query-with-same-score/107018/6 "2017-12-15T10:28:54Z")

</div>

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