tophits-aggregation.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. * <<request-body-search-highlighting,Highlighting>>
  14. * <<request-body-search-explain,Explain>>
  15. * <<request-body-search-queries-and-filters,Named filters and queries>>
  16. * <<request-body-search-source-filtering,Source filtering>>
  17. * <<request-body-search-stored-fields,Stored fields>>
  18. * <<request-body-search-script-fields,Script fields>>
  19. * <<request-body-search-docvalue-fields,Doc value fields>>
  20. * <<request-body-search-version,Include versions>>
  21. * <<request-body-search-seq-no-primary-term,Include Sequence Numbers and Primary Terms>>
  22. ==== Example
  23. In the following example we group the sales by type and per type we show the last sale.
  24. For each sale only the date and price fields are being included in the source.
  25. [source,console]
  26. --------------------------------------------------
  27. POST /sales/_search?size=0
  28. {
  29. "aggs": {
  30. "top_tags": {
  31. "terms": {
  32. "field": "type",
  33. "size": 3
  34. },
  35. "aggs": {
  36. "top_sales_hits": {
  37. "top_hits": {
  38. "sort": [
  39. {
  40. "date": {
  41. "order": "desc"
  42. }
  43. }
  44. ],
  45. "_source": {
  46. "includes": [ "date", "price" ]
  47. },
  48. "size" : 1
  49. }
  50. }
  51. }
  52. }
  53. }
  54. }
  55. --------------------------------------------------
  56. // TEST[setup:sales]
  57. Possible response:
  58. [source,console-result]
  59. --------------------------------------------------
  60. {
  61. ...
  62. "aggregations": {
  63. "top_tags": {
  64. "doc_count_error_upper_bound": 0,
  65. "sum_other_doc_count": 0,
  66. "buckets": [
  67. {
  68. "key": "hat",
  69. "doc_count": 3,
  70. "top_sales_hits": {
  71. "hits": {
  72. "total" : {
  73. "value": 3,
  74. "relation": "eq"
  75. },
  76. "max_score": null,
  77. "hits": [
  78. {
  79. "_index": "sales",
  80. "_id": "AVnNBmauCQpcRyxw6ChK",
  81. "_source": {
  82. "date": "2015/03/01 00:00:00",
  83. "price": 200
  84. },
  85. "sort": [
  86. 1425168000000
  87. ],
  88. "_score": null
  89. }
  90. ]
  91. }
  92. }
  93. },
  94. {
  95. "key": "t-shirt",
  96. "doc_count": 3,
  97. "top_sales_hits": {
  98. "hits": {
  99. "total" : {
  100. "value": 3,
  101. "relation": "eq"
  102. },
  103. "max_score": null,
  104. "hits": [
  105. {
  106. "_index": "sales",
  107. "_id": "AVnNBmauCQpcRyxw6ChL",
  108. "_source": {
  109. "date": "2015/03/01 00:00:00",
  110. "price": 175
  111. },
  112. "sort": [
  113. 1425168000000
  114. ],
  115. "_score": null
  116. }
  117. ]
  118. }
  119. }
  120. },
  121. {
  122. "key": "bag",
  123. "doc_count": 1,
  124. "top_sales_hits": {
  125. "hits": {
  126. "total" : {
  127. "value": 1,
  128. "relation": "eq"
  129. },
  130. "max_score": null,
  131. "hits": [
  132. {
  133. "_index": "sales",
  134. "_id": "AVnNBmatCQpcRyxw6ChH",
  135. "_source": {
  136. "date": "2015/01/01 00:00:00",
  137. "price": 150
  138. },
  139. "sort": [
  140. 1420070400000
  141. ],
  142. "_score": null
  143. }
  144. ]
  145. }
  146. }
  147. }
  148. ]
  149. }
  150. }
  151. }
  152. --------------------------------------------------
  153. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  154. // TESTRESPONSE[s/AVnNBmauCQpcRyxw6ChK/$body.aggregations.top_tags.buckets.0.top_sales_hits.hits.hits.0._id/]
  155. // TESTRESPONSE[s/AVnNBmauCQpcRyxw6ChL/$body.aggregations.top_tags.buckets.1.top_sales_hits.hits.hits.0._id/]
  156. // TESTRESPONSE[s/AVnNBmatCQpcRyxw6ChH/$body.aggregations.top_tags.buckets.2.top_sales_hits.hits.hits.0._id/]
  157. ==== Field collapse example
  158. Field collapsing or result grouping is a feature that logically groups a result set into groups and per group returns
  159. top documents. The ordering of the groups is determined by the relevancy of the first document in a group. In
  160. Elasticsearch this can be implemented via a bucket aggregator that wraps a `top_hits` aggregator as sub-aggregator.
  161. In the example below we search across crawled webpages. For each webpage we store the body and the domain the webpage
  162. belong to. By defining a `terms` aggregator on the `domain` field we group the result set of webpages by domain. The
  163. `top_hits` aggregator is then defined as sub-aggregator, so that the top matching hits are collected per bucket.
  164. Also a `max` aggregator is defined which is used by the `terms` aggregator's order feature to return the buckets by
  165. relevancy order of the most relevant document in a bucket.
  166. [source,console]
  167. --------------------------------------------------
  168. POST /sales/_search
  169. {
  170. "query": {
  171. "match": {
  172. "body": "elections"
  173. }
  174. },
  175. "aggs": {
  176. "top_sites": {
  177. "terms": {
  178. "field": "domain",
  179. "order": {
  180. "top_hit": "desc"
  181. }
  182. },
  183. "aggs": {
  184. "top_tags_hits": {
  185. "top_hits": {}
  186. },
  187. "top_hit" : {
  188. "max": {
  189. "script": {
  190. "source": "_score"
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. --------------------------------------------------
  199. // TEST[setup:sales]
  200. At the moment the `max` (or `min`) aggregator is needed to make sure the buckets from the `terms` aggregator are
  201. ordered according to the score of the most relevant webpage per domain. Unfortunately the `top_hits` aggregator
  202. can't be used in the `order` option of the `terms` aggregator yet.
  203. ==== top_hits support in a nested or reverse_nested aggregator
  204. If the `top_hits` aggregator is wrapped in a `nested` or `reverse_nested` aggregator then nested hits are being returned.
  205. Nested hits are in a sense hidden mini documents that are part of regular document where in the mapping a nested field type
  206. has been configured. The `top_hits` aggregator has the ability to un-hide these documents if it is wrapped in a `nested`
  207. or `reverse_nested` aggregator. Read more about nested in the <<nested,nested type mapping>>.
  208. If nested type has been configured a single document is actually indexed as multiple Lucene documents and they share
  209. 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
  210. nested hits also include their nested identity. The nested identity is kept under the `_nested` field in the search hit
  211. and includes the array field and the offset in the array field the nested hit belongs to. The offset is zero based.
  212. Let's see how it works with a real sample. Considering the following mapping:
  213. [source,console]
  214. --------------------------------------------------
  215. PUT /sales
  216. {
  217. "mappings": {
  218. "properties" : {
  219. "tags" : { "type" : "keyword" },
  220. "comments" : { <1>
  221. "type" : "nested",
  222. "properties" : {
  223. "username" : { "type" : "keyword" },
  224. "comment" : { "type" : "text" }
  225. }
  226. }
  227. }
  228. }
  229. }
  230. --------------------------------------------------
  231. <1> The `comments` is an array that holds nested documents under the `product` object.
  232. And some documents:
  233. [source,console]
  234. --------------------------------------------------
  235. PUT /sales/_doc/1?refresh
  236. {
  237. "tags": ["car", "auto"],
  238. "comments": [
  239. {"username": "baddriver007", "comment": "This car could have better brakes"},
  240. {"username": "dr_who", "comment": "Where's the autopilot? Can't find it"},
  241. {"username": "ilovemotorbikes", "comment": "This car has two extra wheels"}
  242. ]
  243. }
  244. --------------------------------------------------
  245. // TEST[continued]
  246. It's now possible to execute the following `top_hits` aggregation (wrapped in a `nested` aggregation):
  247. [source,console]
  248. --------------------------------------------------
  249. POST /sales/_search
  250. {
  251. "query": {
  252. "term": { "tags": "car" }
  253. },
  254. "aggs": {
  255. "by_sale": {
  256. "nested" : {
  257. "path" : "comments"
  258. },
  259. "aggs": {
  260. "by_user": {
  261. "terms": {
  262. "field": "comments.username",
  263. "size": 1
  264. },
  265. "aggs": {
  266. "by_nested": {
  267. "top_hits":{}
  268. }
  269. }
  270. }
  271. }
  272. }
  273. }
  274. }
  275. --------------------------------------------------
  276. // TEST[continued]
  277. // TEST[s/_search/_search\?filter_path=aggregations.by_sale.by_user.buckets/]
  278. Top hits response snippet with a nested hit, which resides in the first slot of array field `comments`:
  279. [source,console-result]
  280. --------------------------------------------------
  281. {
  282. ...
  283. "aggregations": {
  284. "by_sale": {
  285. "by_user": {
  286. "buckets": [
  287. {
  288. "key": "baddriver007",
  289. "doc_count": 1,
  290. "by_nested": {
  291. "hits": {
  292. "total" : {
  293. "value": 1,
  294. "relation": "eq"
  295. },
  296. "max_score": 0.3616575,
  297. "hits": [
  298. {
  299. "_index": "sales",
  300. "_id": "1",
  301. "_nested": {
  302. "field": "comments", <1>
  303. "offset": 0 <2>
  304. },
  305. "_score": 0.3616575,
  306. "_source": {
  307. "comment": "This car could have better brakes", <3>
  308. "username": "baddriver007"
  309. }
  310. }
  311. ]
  312. }
  313. }
  314. }
  315. ...
  316. ]
  317. }
  318. }
  319. }
  320. }
  321. --------------------------------------------------
  322. // TESTRESPONSE[s/\.\.\.//]
  323. <1> Name of the array field containing the nested hit
  324. <2> Position if the nested hit in the containing array
  325. <3> Source of the nested hit
  326. If `_source` is requested then just the part of the source of the nested object is returned, not the entire source of the document.
  327. Also stored fields on the *nested* inner object level are accessible via `top_hits` aggregator residing in a `nested` or `reverse_nested` aggregator.
  328. Only nested hits will have a `_nested` field in the hit, non nested (regular) hits will not have a `_nested` field.
  329. The information in `_nested` can also be used to parse the original source somewhere else if `_source` isn't enabled.
  330. If there are multiple levels of nested object types defined in mappings then the `_nested` information can also be hierarchical
  331. in order to express the identity of nested hits that are two layers deep or more.
  332. In the example below a nested hit resides in the first slot of the field `nested_grand_child_field` which then resides in
  333. the second slow of the `nested_child_field` field:
  334. [source,js]
  335. --------------------------------------------------
  336. ...
  337. "hits": {
  338. "total" : {
  339. "value": 2565,
  340. "relation": "eq"
  341. },
  342. "max_score": 1,
  343. "hits": [
  344. {
  345. "_index": "a",
  346. "_id": "1",
  347. "_score": 1,
  348. "_nested" : {
  349. "field" : "nested_child_field",
  350. "offset" : 1,
  351. "_nested" : {
  352. "field" : "nested_grand_child_field",
  353. "offset" : 0
  354. }
  355. }
  356. "_source": ...
  357. },
  358. ...
  359. ]
  360. }
  361. ...
  362. --------------------------------------------------
  363. // NOTCONSOLE