tophits-aggregation.asciidoc 13 KB

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