Top Hits Aggregation

A top_hits metric aggregator keeps track of the most relevant document being aggregated. This aggregator is intended to be used as a sub aggregator, so that the top matching documents can be aggregated per bucket.

We do not recommend using top_hits as a top-level aggregation. If you want to group search hits, use the collapse parameter instead.

The top_hits aggregator can effectively be used to group result sets by certain fields via a bucket aggregator. One or more bucket aggregators determines by which properties a result set get sliced into.

Options

  • from - The offset from the first result you want to fetch.
  • size - The maximum number of top matching hits to return per bucket. By default the top three matching hits are returned.
  • sort - How the top matching hits should be sorted. By default the hits are sorted by the score of the main query.

Supported per hit features

The top_hits aggregation returns regular search hits, because of this many per hit features can be supported:

If you only need docvalue_fields, size, and sort then Top Metrics Aggregation might be a more efficient choice than the Top Hits Aggregation.

top_hits does not support the rescore parameter. Query rescoring applies only to search hits, not aggregation results. To change the scores used by aggregations, use a function_score or script_score query.

Example

In the following example we group the sales by type and per type we show the last sale. For each sale only the date and price fields are being included in the source.

  1. POST /sales/_search?size=0
  2. {
  3. "aggs": {
  4. "top_tags": {
  5. "terms": {
  6. "field": "type",
  7. "size": 3
  8. },
  9. "aggs": {
  10. "top_sales_hits": {
  11. "top_hits": {
  12. "sort": [
  13. {
  14. "date": {
  15. "order": "desc"
  16. }
  17. }
  18. ],
  19. "_source": {
  20. "includes": [ "date", "price" ]
  21. },
  22. "size": 1
  23. }
  24. }
  25. }
  26. }
  27. }
  28. }

Possible response:

  1. {
  2. ...
  3. "aggregations": {
  4. "top_tags": {
  5. "doc_count_error_upper_bound": 0,
  6. "sum_other_doc_count": 0,
  7. "buckets": [
  8. {
  9. "key": "hat",
  10. "doc_count": 3,
  11. "top_sales_hits": {
  12. "hits": {
  13. "total" : {
  14. "value": 3,
  15. "relation": "eq"
  16. },
  17. "max_score": null,
  18. "hits": [
  19. {
  20. "_index": "sales",
  21. "_type": "_doc",
  22. "_id": "AVnNBmauCQpcRyxw6ChK",
  23. "_source": {
  24. "date": "2015/03/01 00:00:00",
  25. "price": 200
  26. },
  27. "sort": [
  28. 1425168000000
  29. ],
  30. "_score": null
  31. }
  32. ]
  33. }
  34. }
  35. },
  36. {
  37. "key": "t-shirt",
  38. "doc_count": 3,
  39. "top_sales_hits": {
  40. "hits": {
  41. "total" : {
  42. "value": 3,
  43. "relation": "eq"
  44. },
  45. "max_score": null,
  46. "hits": [
  47. {
  48. "_index": "sales",
  49. "_type": "_doc",
  50. "_id": "AVnNBmauCQpcRyxw6ChL",
  51. "_source": {
  52. "date": "2015/03/01 00:00:00",
  53. "price": 175
  54. },
  55. "sort": [
  56. 1425168000000
  57. ],
  58. "_score": null
  59. }
  60. ]
  61. }
  62. }
  63. },
  64. {
  65. "key": "bag",
  66. "doc_count": 1,
  67. "top_sales_hits": {
  68. "hits": {
  69. "total" : {
  70. "value": 1,
  71. "relation": "eq"
  72. },
  73. "max_score": null,
  74. "hits": [
  75. {
  76. "_index": "sales",
  77. "_type": "_doc",
  78. "_id": "AVnNBmatCQpcRyxw6ChH",
  79. "_source": {
  80. "date": "2015/01/01 00:00:00",
  81. "price": 150
  82. },
  83. "sort": [
  84. 1420070400000
  85. ],
  86. "_score": null
  87. }
  88. ]
  89. }
  90. }
  91. }
  92. ]
  93. }
  94. }
  95. }

Field collapse example

Field collapsing or result grouping is a feature that logically groups a result set into groups and per group returns top documents. The ordering of the groups is determined by the relevancy of the first document in a group. In Elasticsearch this can be implemented via a bucket aggregator that wraps a top_hits aggregator as sub-aggregator.

In the example below we search across crawled webpages. For each webpage we store the body and the domain the webpage belong to. By defining a terms aggregator on the domain field we group the result set of webpages by domain. The top_hits aggregator is then defined as sub-aggregator, so that the top matching hits are collected per bucket.

Also a max aggregator is defined which is used by the terms aggregator’s order feature to return the buckets by relevancy order of the most relevant document in a bucket.

  1. POST /sales/_search
  2. {
  3. "query": {
  4. "match": {
  5. "body": "elections"
  6. }
  7. },
  8. "aggs": {
  9. "top_sites": {
  10. "terms": {
  11. "field": "domain",
  12. "order": {
  13. "top_hit": "desc"
  14. }
  15. },
  16. "aggs": {
  17. "top_tags_hits": {
  18. "top_hits": {}
  19. },
  20. "top_hit" : {
  21. "max": {
  22. "script": {
  23. "source": "_score"
  24. }
  25. }
  26. }
  27. }
  28. }
  29. }
  30. }

At the moment the max (or min) aggregator is needed to make sure the buckets from the terms aggregator are ordered according to the score of the most relevant webpage per domain. Unfortunately the top_hits aggregator can’t be used in the order option of the terms aggregator yet.

top_hits support in a nested or reverse_nested aggregator

If the top_hits aggregator is wrapped in a nested or reverse_nested aggregator then nested hits are being returned. Nested hits are in a sense hidden mini documents that are part of regular document where in the mapping a nested field type has been configured. The top_hits aggregator has the ability to un-hide these documents if it is wrapped in a nested or reverse_nested aggregator. Read more about nested in the nested type mapping.

If nested type has been configured a single document is actually indexed as multiple Lucene documents and they share the same id. In order to determine the identity of a nested hit there is more needed than just the id, so that is why nested hits also include their nested identity. The nested identity is kept under the _nested field in the search hit and includes the array field and the offset in the array field the nested hit belongs to. The offset is zero based.

Let’s see how it works with a real sample. Considering the following mapping:

  1. PUT /sales
  2. {
  3. "mappings": {
  4. "properties": {
  5. "tags": { "type": "keyword" },
  6. "comments": {
  7. "type": "nested",
  8. "properties": {
  9. "username": { "type": "keyword" },
  10. "comment": { "type": "text" }
  11. }
  12. }
  13. }
  14. }
  15. }

The comments is an array that holds nested documents under the product object.

And some documents:

  1. PUT /sales/_doc/1?refresh
  2. {
  3. "tags": [ "car", "auto" ],
  4. "comments": [
  5. { "username": "baddriver007", "comment": "This car could have better brakes" },
  6. { "username": "dr_who", "comment": "Where's the autopilot? Can't find it" },
  7. { "username": "ilovemotorbikes", "comment": "This car has two extra wheels" }
  8. ]
  9. }

It’s now possible to execute the following top_hits aggregation (wrapped in a nested aggregation):

  1. POST /sales/_search
  2. {
  3. "query": {
  4. "term": { "tags": "car" }
  5. },
  6. "aggs": {
  7. "by_sale": {
  8. "nested": {
  9. "path": "comments"
  10. },
  11. "aggs": {
  12. "by_user": {
  13. "terms": {
  14. "field": "comments.username",
  15. "size": 1
  16. },
  17. "aggs": {
  18. "by_nested": {
  19. "top_hits": {}
  20. }
  21. }
  22. }
  23. }
  24. }
  25. }
  26. }

Top hits response snippet with a nested hit, which resides in the first slot of array field comments:

  1. {
  2. ...
  3. "aggregations": {
  4. "by_sale": {
  5. "by_user": {
  6. "buckets": [
  7. {
  8. "key": "baddriver007",
  9. "doc_count": 1,
  10. "by_nested": {
  11. "hits": {
  12. "total" : {
  13. "value": 1,
  14. "relation": "eq"
  15. },
  16. "max_score": 0.3616575,
  17. "hits": [
  18. {
  19. "_index": "sales",
  20. "_type" : "_doc",
  21. "_id": "1",
  22. "_nested": {
  23. "field": "comments",
  24. "offset": 0
  25. },
  26. "_score": 0.3616575,
  27. "_source": {
  28. "comment": "This car could have better brakes",
  29. "username": "baddriver007"
  30. }
  31. }
  32. ]
  33. }
  34. }
  35. }
  36. ...
  37. ]
  38. }
  39. }
  40. }
  41. }

Name of the array field containing the nested hit

Position if the nested hit in the containing array

Source of the nested hit

If _source is requested then just the part of the source of the nested object is returned, not the entire source of the document. Also stored fields on the nested inner object level are accessible via top_hits aggregator residing in a nested or reverse_nested aggregator.

Only nested hits will have a _nested field in the hit, non nested (regular) hits will not have a _nested field.

The information in _nested can also be used to parse the original source somewhere else if _source isn’t enabled.

If there are multiple levels of nested object types defined in mappings then the _nested information can also be hierarchical in order to express the identity of nested hits that are two layers deep or more.

In the example below a nested hit resides in the first slot of the field nested_grand_child_field which then resides in the second slow of the nested_child_field field:

  1. ...
  2. "hits": {
  3. "total" : {
  4. "value": 2565,
  5. "relation": "eq"
  6. },
  7. "max_score": 1,
  8. "hits": [
  9. {
  10. "_index": "a",
  11. "_type": "b",
  12. "_id": "1",
  13. "_score": 1,
  14. "_nested" : {
  15. "field" : "nested_child_field",
  16. "offset" : 1,
  17. "_nested" : {
  18. "field" : "nested_grand_child_field",
  19. "offset" : 0
  20. }
  21. }
  22. "_source": ...
  23. },
  24. ...
  25. ]
  26. }
  27. ...