top-metrics-aggregation.asciidoc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[search-aggregations-metrics-top-metrics]]
  4. === Top metrics aggregation
  5. ++++
  6. <titleabbrev>Top metrics</titleabbrev>
  7. ++++
  8. The `top_metrics` aggregation selects metrics from the document with the largest or smallest "sort"
  9. value. For example, this gets the value of the `m` field on the document with the largest value of `s`:
  10. [source,console,id=search-aggregations-metrics-top-metrics-simple]
  11. ----
  12. POST /test/_bulk?refresh
  13. {"index": {}}
  14. {"s": 1, "m": 3.1415}
  15. {"index": {}}
  16. {"s": 2, "m": 1.0}
  17. {"index": {}}
  18. {"s": 3, "m": 2.71828}
  19. POST /test/_search?filter_path=aggregations
  20. {
  21. "aggs": {
  22. "tm": {
  23. "top_metrics": {
  24. "metrics": {"field": "m"},
  25. "sort": {"s": "desc"}
  26. }
  27. }
  28. }
  29. }
  30. ----
  31. Which returns:
  32. [source,js]
  33. ----
  34. {
  35. "aggregations": {
  36. "tm": {
  37. "top": [ {"sort": [3], "metrics": {"m": 2.718280076980591 } } ]
  38. }
  39. }
  40. }
  41. ----
  42. // TESTRESPONSE
  43. `top_metrics` is fairly similar to <<search-aggregations-metrics-top-hits-aggregation, `top_hits`>>
  44. in spirit but because it is more limited it is able to do its job using less memory and is often
  45. faster.
  46. ==== `sort`
  47. The `sort` field in the metric request functions exactly the same as the `sort` field in the
  48. <<sort-search-results, search>> request except:
  49. * It can't be used on <<binary,binary>>, <<flattened,flattened>>, <<ip,ip>>,
  50. <<keyword,keyword>>, or <<text,text>> fields.
  51. * It only supports a single sort value so which document wins ties is not specified.
  52. The metrics that the aggregation returns is the first hit that would be returned by the search
  53. request. So,
  54. `"sort": {"s": "desc"}`:: gets metrics from the document with the highest `s`
  55. `"sort": {"s": "asc"}`:: gets the metrics from the document with the lowest `s`
  56. `"sort": {"_geo_distance": {"location": "35.7796, -78.6382"}}`::
  57. gets metrics from the documents with `location` *closest* to `35.7796, -78.6382`
  58. `"sort": "_score"`:: gets metrics from the document with the highest score
  59. ==== `metrics`
  60. `metrics` selects the fields of the "top" document to return. You can request
  61. a single metric with something like `"metrics": {"field": "m"}` or multiple
  62. metrics by requesting a list of metrics like `"metrics": [{"field": "m"}, {"field": "i"}`.
  63. `metrics.field` supports the following field types:
  64. * <<boolean,`boolean`>>
  65. * <<ip,`ip`>>
  66. * <<keyword,keywords>>
  67. * <<number,numbers>>
  68. Except for keywords, <<runtime,runtime fields>> for corresponding types are also
  69. supported. `metrics.field` doesn't support fields with <<array,array values>>. A
  70. `top_metric` aggregation on array values may return inconsistent results.
  71. The following example runs a `top_metrics` aggregation on several field types.
  72. [source,console,id=search-aggregations-metrics-top-metrics-list-of-metrics]
  73. ----
  74. PUT /test
  75. {
  76. "mappings": {
  77. "properties": {
  78. "d": {"type": "date"}
  79. }
  80. }
  81. }
  82. POST /test/_bulk?refresh
  83. {"index": {}}
  84. {"s": 1, "m": 3.1415, "i": 1, "d": "2020-01-01T00:12:12Z", "t": "cat"}
  85. {"index": {}}
  86. {"s": 2, "m": 1.0, "i": 6, "d": "2020-01-02T00:12:12Z", "t": "dog"}
  87. {"index": {}}
  88. {"s": 3, "m": 2.71828, "i": -12, "d": "2019-12-31T00:12:12Z", "t": "chicken"}
  89. POST /test/_search?filter_path=aggregations
  90. {
  91. "aggs": {
  92. "tm": {
  93. "top_metrics": {
  94. "metrics": [
  95. {"field": "m"},
  96. {"field": "i"},
  97. {"field": "d"},
  98. {"field": "t.keyword"}
  99. ],
  100. "sort": {"s": "desc"}
  101. }
  102. }
  103. }
  104. }
  105. ----
  106. Which returns:
  107. [source,js]
  108. ----
  109. {
  110. "aggregations": {
  111. "tm": {
  112. "top": [ {
  113. "sort": [3],
  114. "metrics": {
  115. "m": 2.718280076980591,
  116. "i": -12,
  117. "d": "2019-12-31T00:12:12.000Z",
  118. "t.keyword": "chicken"
  119. }
  120. } ]
  121. }
  122. }
  123. }
  124. ----
  125. // TESTRESPONSE
  126. ==== `size`
  127. `top_metrics` can return the top few document's worth of metrics using the size parameter:
  128. [source,console,id=search-aggregations-metrics-top-metrics-size]
  129. ----
  130. POST /test/_bulk?refresh
  131. {"index": {}}
  132. {"s": 1, "m": 3.1415}
  133. {"index": {}}
  134. {"s": 2, "m": 1.0}
  135. {"index": {}}
  136. {"s": 3, "m": 2.71828}
  137. POST /test/_search?filter_path=aggregations
  138. {
  139. "aggs": {
  140. "tm": {
  141. "top_metrics": {
  142. "metrics": {"field": "m"},
  143. "sort": {"s": "desc"},
  144. "size": 3
  145. }
  146. }
  147. }
  148. }
  149. ----
  150. Which returns:
  151. [source,js]
  152. ----
  153. {
  154. "aggregations": {
  155. "tm": {
  156. "top": [
  157. {"sort": [3], "metrics": {"m": 2.718280076980591 } },
  158. {"sort": [2], "metrics": {"m": 1.0 } },
  159. {"sort": [1], "metrics": {"m": 3.1414999961853027 } }
  160. ]
  161. }
  162. }
  163. }
  164. ----
  165. // TESTRESPONSE
  166. The default `size` is 1. The maximum default size is `10` because the aggregation's
  167. working storage is "dense", meaning we allocate `size` slots for every bucket. `10`
  168. is a *very* conservative default maximum and you can raise it if you need to by
  169. changing the `top_metrics_max_size` index setting. But know that large sizes can
  170. take a fair bit of memory, especially if they are inside of an aggregation which
  171. makes many buckes like a large
  172. <<search-aggregations-metrics-top-metrics-example-terms, terms aggregation>>. If
  173. you till want to raise it, use something like:
  174. [source,console]
  175. ----
  176. PUT /test/_settings
  177. {
  178. "top_metrics_max_size": 100
  179. }
  180. ----
  181. // TEST[continued]
  182. NOTE: If `size` is more than `1` the `top_metrics` aggregation can't be the *target* of a sort.
  183. ==== Examples
  184. [[search-aggregations-metrics-top-metrics-example-terms]]
  185. ===== Use with terms
  186. This aggregation should be quite useful inside of <<search-aggregations-bucket-terms-aggregation, `terms`>>
  187. aggregation, to, say, find the last value reported by each server.
  188. [source,console,id=search-aggregations-metrics-top-metrics-terms]
  189. ----
  190. PUT /node
  191. {
  192. "mappings": {
  193. "properties": {
  194. "ip": {"type": "ip"},
  195. "date": {"type": "date"}
  196. }
  197. }
  198. }
  199. POST /node/_bulk?refresh
  200. {"index": {}}
  201. {"ip": "192.168.0.1", "date": "2020-01-01T01:01:01", "m": 1}
  202. {"index": {}}
  203. {"ip": "192.168.0.1", "date": "2020-01-01T02:01:01", "m": 2}
  204. {"index": {}}
  205. {"ip": "192.168.0.2", "date": "2020-01-01T02:01:01", "m": 3}
  206. POST /node/_search?filter_path=aggregations
  207. {
  208. "aggs": {
  209. "ip": {
  210. "terms": {
  211. "field": "ip"
  212. },
  213. "aggs": {
  214. "tm": {
  215. "top_metrics": {
  216. "metrics": {"field": "m"},
  217. "sort": {"date": "desc"}
  218. }
  219. }
  220. }
  221. }
  222. }
  223. }
  224. ----
  225. Which returns:
  226. [source,js]
  227. ----
  228. {
  229. "aggregations": {
  230. "ip": {
  231. "buckets": [
  232. {
  233. "key": "192.168.0.1",
  234. "doc_count": 2,
  235. "tm": {
  236. "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"m": 2 } } ]
  237. }
  238. },
  239. {
  240. "key": "192.168.0.2",
  241. "doc_count": 1,
  242. "tm": {
  243. "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"m": 3 } } ]
  244. }
  245. }
  246. ],
  247. "doc_count_error_upper_bound": 0,
  248. "sum_other_doc_count": 0
  249. }
  250. }
  251. }
  252. ----
  253. // TESTRESPONSE
  254. Unlike `top_hits`, you can sort buckets by the results of this metric:
  255. [source,console]
  256. ----
  257. POST /node/_search?filter_path=aggregations
  258. {
  259. "aggs": {
  260. "ip": {
  261. "terms": {
  262. "field": "ip",
  263. "order": {"tm.m": "desc"}
  264. },
  265. "aggs": {
  266. "tm": {
  267. "top_metrics": {
  268. "metrics": {"field": "m"},
  269. "sort": {"date": "desc"}
  270. }
  271. }
  272. }
  273. }
  274. }
  275. }
  276. ----
  277. // TEST[continued]
  278. Which returns:
  279. [source,js]
  280. ----
  281. {
  282. "aggregations": {
  283. "ip": {
  284. "buckets": [
  285. {
  286. "key": "192.168.0.2",
  287. "doc_count": 1,
  288. "tm": {
  289. "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"m": 3 } } ]
  290. }
  291. },
  292. {
  293. "key": "192.168.0.1",
  294. "doc_count": 2,
  295. "tm": {
  296. "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"m": 2 } } ]
  297. }
  298. }
  299. ],
  300. "doc_count_error_upper_bound": 0,
  301. "sum_other_doc_count": 0
  302. }
  303. }
  304. }
  305. ----
  306. // TESTRESPONSE
  307. ===== Mixed sort types
  308. Sorting `top_metrics` by a field that has different types across different
  309. indices producs somewhat surprising results: floating point fields are
  310. always sorted independently of whole numbered fields.
  311. [source,console,id=search-aggregations-metrics-top-metrics-mixed-sort]
  312. ----
  313. POST /test/_bulk?refresh
  314. {"index": {"_index": "test1"}}
  315. {"s": 1, "m": 3.1415}
  316. {"index": {"_index": "test1"}}
  317. {"s": 2, "m": 1}
  318. {"index": {"_index": "test2"}}
  319. {"s": 3.1, "m": 2.71828}
  320. POST /test*/_search?filter_path=aggregations
  321. {
  322. "aggs": {
  323. "tm": {
  324. "top_metrics": {
  325. "metrics": {"field": "m"},
  326. "sort": {"s": "asc"}
  327. }
  328. }
  329. }
  330. }
  331. ----
  332. Which returns:
  333. [source,js]
  334. ----
  335. {
  336. "aggregations": {
  337. "tm": {
  338. "top": [ {"sort": [3.0999999046325684], "metrics": {"m": 2.718280076980591 } } ]
  339. }
  340. }
  341. }
  342. ----
  343. // TESTRESPONSE
  344. While this is better than an error it *probably* isn't what you were going for.
  345. While it does lose some precision, you can explicitly cast the whole number
  346. fields to floating points with something like:
  347. [source,console]
  348. ----
  349. POST /test*/_search?filter_path=aggregations
  350. {
  351. "aggs": {
  352. "tm": {
  353. "top_metrics": {
  354. "metrics": {"field": "m"},
  355. "sort": {"s": {"order": "asc", "numeric_type": "double"}}
  356. }
  357. }
  358. }
  359. }
  360. ----
  361. // TEST[continued]
  362. Which returns the much more expected:
  363. [source,js]
  364. ----
  365. {
  366. "aggregations": {
  367. "tm": {
  368. "top": [ {"sort": [1.0], "metrics": {"m": 3.1414999961853027 } } ]
  369. }
  370. }
  371. }
  372. ----
  373. // TESTRESPONSE