tophits-aggregation.asciidoc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. [[search-aggregations-metrics-top-hits-aggregation]]
  2. === Top hits Aggregation
  3. A `top_hits` metric aggregator keeps track of the most relevant document being aggregated. This aggregator is intended
  4. to be used as a sub aggregator, so that the top matching documents can be aggregated per bucket.
  5. The `top_hits` aggregator can effectively be used to group result sets by certain fields via a bucket aggregator.
  6. One or more bucket aggregators determines by which properties a result set get sliced into.
  7. ==== Options
  8. * `from` - The offset from the first result you want to fetch.
  9. * `size` - The maximum number of top matching hits to return per bucket. By default the top three matching hits are returned.
  10. * `sort` - How the top matching hits should be sorted. By default the hits are sorted by the score of the main query.
  11. ==== Supported per hit features
  12. The top_hits aggregation returns regular search hits, because of this many per hit features can be supported:
  13. * <<search-request-highlighting,Highlighting>>
  14. * <<search-request-explain,Explain>>
  15. * <<search-request-named-queries-and-filters,Named filters and queries>>
  16. * <<search-request-source-filtering,Source filtering>>
  17. * <<search-request-script-fields,Script fields>>
  18. * <<search-request-fielddata-fields,Fielddata fields>>
  19. * <<search-request-version,Include versions>>
  20. ==== Example
  21. In the following example we group the questions by tag and per tag we show the last active question. For each question
  22. only the title field is being included in the source.
  23. [source,js]
  24. --------------------------------------------------
  25. {
  26. "aggs": {
  27. "top-tags": {
  28. "terms": {
  29. "field": "tags",
  30. "size": 3
  31. },
  32. "aggs": {
  33. "top_tag_hits": {
  34. "top_hits": {
  35. "sort": [
  36. {
  37. "last_activity_date": {
  38. "order": "desc"
  39. }
  40. }
  41. ],
  42. "_source": {
  43. "include": [
  44. "title"
  45. ]
  46. },
  47. "size" : 1
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
  54. --------------------------------------------------
  55. Possible response snippet:
  56. [source,js]
  57. --------------------------------------------------
  58. "aggregations": {
  59. "top-tags": {
  60. "buckets": [
  61. {
  62. "key": "windows-7",
  63. "doc_count": 25365,
  64. "top_tags_hits": {
  65. "hits": {
  66. "total": 25365,
  67. "max_score": 1,
  68. "hits": [
  69. {
  70. "_index": "stack",
  71. "_type": "question",
  72. "_id": "602679",
  73. "_score": 1,
  74. "_source": {
  75. "title": "Windows port opening"
  76. },
  77. "sort": [
  78. 1370143231177
  79. ]
  80. }
  81. ]
  82. }
  83. }
  84. },
  85. {
  86. "key": "linux",
  87. "doc_count": 18342,
  88. "top_tags_hits": {
  89. "hits": {
  90. "total": 18342,
  91. "max_score": 1,
  92. "hits": [
  93. {
  94. "_index": "stack",
  95. "_type": "question",
  96. "_id": "602672",
  97. "_score": 1,
  98. "_source": {
  99. "title": "Ubuntu RFID Screensaver lock-unlock"
  100. },
  101. "sort": [
  102. 1370143379747
  103. ]
  104. }
  105. ]
  106. }
  107. }
  108. },
  109. {
  110. "key": "windows",
  111. "doc_count": 18119,
  112. "top_tags_hits": {
  113. "hits": {
  114. "total": 18119,
  115. "max_score": 1,
  116. "hits": [
  117. {
  118. "_index": "stack",
  119. "_type": "question",
  120. "_id": "602678",
  121. "_score": 1,
  122. "_source": {
  123. "title": "If I change my computers date / time, what could be affected?"
  124. },
  125. "sort": [
  126. 1370142868283
  127. ]
  128. }
  129. ]
  130. }
  131. }
  132. }
  133. ]
  134. }
  135. }
  136. --------------------------------------------------
  137. ==== Field collapse example
  138. Field collapsing or result grouping is a feature that logically groups a result set into groups and per group returns
  139. top documents. The ordering of the groups is determined by the relevancy of the first document in a group. In
  140. Elasticsearch this can be implemented via a bucket aggregator that wraps a `top_hits` aggregator as sub-aggregator.
  141. In the example below we search across crawled webpages. For each webpage we store the body and the domain the webpage
  142. belong to. By defining a `terms` aggregator on the `domain` field we group the result set of webpages by domain. The
  143. `top_docs` aggregator is then defined as sub-aggregator, so that the top matching hits are collected per bucket.
  144. Also a `max` aggregator is defined which is used by the `terms` aggregator's order feature the return the buckets by
  145. relevancy order of the most relevant document in a bucket.
  146. [source,js]
  147. --------------------------------------------------
  148. {
  149. "query": {
  150. "match": {
  151. "body": "elections"
  152. }
  153. },
  154. "aggs": {
  155. "top-sites": {
  156. "terms": {
  157. "field": "domain",
  158. "order": {
  159. "top_hit": "desc"
  160. }
  161. },
  162. "aggs": {
  163. "top_tags_hits": {
  164. "top_hits": {}
  165. },
  166. "top_hit" : {
  167. "max": {
  168. "script": "_score"
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. --------------------------------------------------
  176. At the moment the `max` (or `min`) aggregator is needed to make sure the buckets from the `terms` aggregator are
  177. ordered according to the score of the most relevant webpage per domain. The `top_hits` aggregator isn't a metric aggregator
  178. and therefore can't be used in the `order` option of the `terms` aggregator.