1
0

tophits-aggregation.asciidoc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.
  179. ==== top_hits support in a nested or reverse_nested aggregator
  180. If the `top_hits` aggregator is wrapped in a `nested` or `reverse_nested` aggregator then nested hits are being returned.
  181. Nested hits are in a sense hidden mini documents that are part of regular document where in the mapping a nested field type
  182. has been configured. The `top_hits` aggregator has the ability to un-hide these documents if it is wrapped in a `nested`
  183. or `reverse_nested` aggregator. Read more about nested in the <<mapping-nested-type,nested type mapping>>.
  184. If nested type has been configured a single document is actually indexed as multiple Lucene documents and they share
  185. 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
  186. nested hits also include their nested identity. The nested identity is kept under the `_nested` field in the search hit
  187. and includes the array field and the offset in the array field the nested hit belongs to. The offset is zero based.
  188. Top hits response snippet with a nested hit, which resides in the third slot of array field `nested_field1` in document with id `1`:
  189. [source,js]
  190. --------------------------------------------------
  191. ...
  192. "hits": {
  193. "total": 25365,
  194. "max_score": 1,
  195. "hits": [
  196. {
  197. "_index": "a",
  198. "_type": "b",
  199. "_id": "1",
  200. "_score": 1,
  201. "_nested" : {
  202. "field" : "nested_field1",
  203. "offset" : 2
  204. }
  205. "_source": ...
  206. },
  207. ...
  208. ]
  209. }
  210. ...
  211. --------------------------------------------------
  212. If `_source` is requested then just the part of the source of the nested object is returned, not the entire source of the document.
  213. Also stored fields on the *nested* inner object level are accessible via `top_hits` aggregator residing in a `nested` or `reverse_nested` aggregator.
  214. Only nested hits will have a `_nested` field in the hit, non nested (regular) hits will not have a `_nested` field.
  215. The information in `_nested` can also be used to parse the original source somewhere else if `_source` isn't enabled.
  216. If there are multiple levels of nested object types defined in mappings then the `_nested` information can also be hierarchical
  217. in order to express the identity of nested hits that are two layers deep or more.
  218. In the example below a nested hit resides in the first slot of the field `nested_grand_child_field` which then resides in
  219. the second slow of the `nested_child_field` field:
  220. [source,js]
  221. --------------------------------------------------
  222. ...
  223. "hits": {
  224. "total": 2565,
  225. "max_score": 1,
  226. "hits": [
  227. {
  228. "_index": "a",
  229. "_type": "b",
  230. "_id": "1",
  231. "_score": 1,
  232. "_nested" : {
  233. "field" : "nested_child_field",
  234. "offset" : 1,
  235. "_nested" : {
  236. "field" : "nested_grand_child_field",
  237. "offset" : 0
  238. }
  239. }
  240. "_source": ...
  241. },
  242. ...
  243. ]
  244. }
  245. ...
  246. --------------------------------------------------