1
0

tophits-aggregation.asciidoc 13 KB

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