top-metrics-aggregation.asciidoc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 `"metric": {"field": "m"}` or multiple
  62. metrics by requesting a list of metrics like `"metric": [{"field": "m"}, {"field": "i"}`.
  63. The fields can be <<number,numbers>>, <<keyword,keywords>>, or <<ip,ips>>.
  64. Here is a more complete example:
  65. [source,console,id=search-aggregations-metrics-top-metrics-list-of-metrics]
  66. ----
  67. PUT /test
  68. {
  69. "mappings": {
  70. "properties": {
  71. "d": {"type": "date"}
  72. }
  73. }
  74. }
  75. POST /test/_bulk?refresh
  76. {"index": {}}
  77. {"s": 1, "m": 3.1415, "i": 1, "d": "2020-01-01T00:12:12Z", "t": "cat"}
  78. {"index": {}}
  79. {"s": 2, "m": 1.0, "i": 6, "d": "2020-01-02T00:12:12Z", "t": "dog"}
  80. {"index": {}}
  81. {"s": 3, "m": 2.71828, "i": -12, "d": "2019-12-31T00:12:12Z", "t": "chicken"}
  82. POST /test/_search?filter_path=aggregations
  83. {
  84. "aggs": {
  85. "tm": {
  86. "top_metrics": {
  87. "metrics": [
  88. {"field": "m"},
  89. {"field": "i"},
  90. {"field": "d"},
  91. {"field": "t.keyword"}
  92. ],
  93. "sort": {"s": "desc"}
  94. }
  95. }
  96. }
  97. }
  98. ----
  99. Which returns:
  100. [source,js]
  101. ----
  102. {
  103. "aggregations": {
  104. "tm": {
  105. "top": [ {
  106. "sort": [3],
  107. "metrics": {
  108. "m": 2.718280076980591,
  109. "i": -12,
  110. "d": "2019-12-31T00:12:12.000Z",
  111. "t.keyword": "chicken"
  112. }
  113. } ]
  114. }
  115. }
  116. }
  117. ----
  118. // TESTRESPONSE
  119. ==== `size`
  120. `top_metrics` can return the top few document's worth of metrics using the size parameter:
  121. [source,console,id=search-aggregations-metrics-top-metrics-size]
  122. ----
  123. POST /test/_bulk?refresh
  124. {"index": {}}
  125. {"s": 1, "m": 3.1415}
  126. {"index": {}}
  127. {"s": 2, "m": 1.0}
  128. {"index": {}}
  129. {"s": 3, "m": 2.71828}
  130. POST /test/_search?filter_path=aggregations
  131. {
  132. "aggs": {
  133. "tm": {
  134. "top_metrics": {
  135. "metrics": {"field": "m"},
  136. "sort": {"s": "desc"},
  137. "size": 3
  138. }
  139. }
  140. }
  141. }
  142. ----
  143. Which returns:
  144. [source,js]
  145. ----
  146. {
  147. "aggregations": {
  148. "tm": {
  149. "top": [
  150. {"sort": [3], "metrics": {"m": 2.718280076980591 } },
  151. {"sort": [2], "metrics": {"m": 1.0 } },
  152. {"sort": [1], "metrics": {"m": 3.1414999961853027 } }
  153. ]
  154. }
  155. }
  156. }
  157. ----
  158. // TESTRESPONSE
  159. The default `size` is 1. The maximum default size is `10` because the aggregation's
  160. working storage is "dense", meaning we allocate `size` slots for every bucket. `10`
  161. is a *very* conservative default maximum and you can raise it if you need to by
  162. changing the `top_metrics_max_size` index setting. But know that large sizes can
  163. take a fair bit of memory, especially if they are inside of an aggregation which
  164. makes many buckes like a large
  165. <<search-aggregations-metrics-top-metrics-example-terms, terms aggregation>>. If
  166. you till want to raise it, use something like:
  167. [source,console]
  168. ----
  169. PUT /test/_settings
  170. {
  171. "top_metrics_max_size": 100
  172. }
  173. ----
  174. // TEST[continued]
  175. NOTE: If `size` is more than `1` the `top_metrics` aggregation can't be the *target* of a sort.
  176. ==== Examples
  177. [[search-aggregations-metrics-top-metrics-example-terms]]
  178. ===== Use with terms
  179. This aggregation should be quite useful inside of <<search-aggregations-bucket-terms-aggregation, `terms`>>
  180. aggregation, to, say, find the last value reported by each server.
  181. [source,console,id=search-aggregations-metrics-top-metrics-terms]
  182. ----
  183. PUT /node
  184. {
  185. "mappings": {
  186. "properties": {
  187. "ip": {"type": "ip"},
  188. "date": {"type": "date"}
  189. }
  190. }
  191. }
  192. POST /node/_bulk?refresh
  193. {"index": {}}
  194. {"ip": "192.168.0.1", "date": "2020-01-01T01:01:01", "m": 1}
  195. {"index": {}}
  196. {"ip": "192.168.0.1", "date": "2020-01-01T02:01:01", "m": 2}
  197. {"index": {}}
  198. {"ip": "192.168.0.2", "date": "2020-01-01T02:01:01", "m": 3}
  199. POST /node/_search?filter_path=aggregations
  200. {
  201. "aggs": {
  202. "ip": {
  203. "terms": {
  204. "field": "ip"
  205. },
  206. "aggs": {
  207. "tm": {
  208. "top_metrics": {
  209. "metrics": {"field": "m"},
  210. "sort": {"date": "desc"}
  211. }
  212. }
  213. }
  214. }
  215. }
  216. }
  217. ----
  218. Which returns:
  219. [source,js]
  220. ----
  221. {
  222. "aggregations": {
  223. "ip": {
  224. "buckets": [
  225. {
  226. "key": "192.168.0.1",
  227. "doc_count": 2,
  228. "tm": {
  229. "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"m": 2 } } ]
  230. }
  231. },
  232. {
  233. "key": "192.168.0.2",
  234. "doc_count": 1,
  235. "tm": {
  236. "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"m": 3 } } ]
  237. }
  238. }
  239. ],
  240. "doc_count_error_upper_bound": 0,
  241. "sum_other_doc_count": 0
  242. }
  243. }
  244. }
  245. ----
  246. // TESTRESPONSE
  247. Unlike `top_hits`, you can sort buckets by the results of this metric:
  248. [source,console]
  249. ----
  250. POST /node/_search?filter_path=aggregations
  251. {
  252. "aggs": {
  253. "ip": {
  254. "terms": {
  255. "field": "ip",
  256. "order": {"tm.m": "desc"}
  257. },
  258. "aggs": {
  259. "tm": {
  260. "top_metrics": {
  261. "metrics": {"field": "m"},
  262. "sort": {"date": "desc"}
  263. }
  264. }
  265. }
  266. }
  267. }
  268. }
  269. ----
  270. // TEST[continued]
  271. Which returns:
  272. [source,js]
  273. ----
  274. {
  275. "aggregations": {
  276. "ip": {
  277. "buckets": [
  278. {
  279. "key": "192.168.0.2",
  280. "doc_count": 1,
  281. "tm": {
  282. "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"m": 3 } } ]
  283. }
  284. },
  285. {
  286. "key": "192.168.0.1",
  287. "doc_count": 2,
  288. "tm": {
  289. "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"m": 2 } } ]
  290. }
  291. }
  292. ],
  293. "doc_count_error_upper_bound": 0,
  294. "sum_other_doc_count": 0
  295. }
  296. }
  297. }
  298. ----
  299. // TESTRESPONSE
  300. ===== Mixed sort types
  301. Sorting `top_metrics` by a field that has different types across different
  302. indices producs somewhat suprising results: floating point fields are
  303. always sorted independantly of whole numbered fields.
  304. [source,console,id=search-aggregations-metrics-top-metrics-mixed-sort]
  305. ----
  306. POST /test/_bulk?refresh
  307. {"index": {"_index": "test1"}}
  308. {"s": 1, "m": 3.1415}
  309. {"index": {"_index": "test1"}}
  310. {"s": 2, "m": 1}
  311. {"index": {"_index": "test2"}}
  312. {"s": 3.1, "m": 2.71828}
  313. POST /test*/_search?filter_path=aggregations
  314. {
  315. "aggs": {
  316. "tm": {
  317. "top_metrics": {
  318. "metrics": {"field": "m"},
  319. "sort": {"s": "asc"}
  320. }
  321. }
  322. }
  323. }
  324. ----
  325. Which returns:
  326. [source,js]
  327. ----
  328. {
  329. "aggregations": {
  330. "tm": {
  331. "top": [ {"sort": [3.0999999046325684], "metrics": {"m": 2.718280076980591 } } ]
  332. }
  333. }
  334. }
  335. ----
  336. // TESTRESPONSE
  337. While this is better than an error it *probably* isn't what you were going for.
  338. While it does lose some precision, you can explictly cast the whole number
  339. fields to floating points with something like:
  340. [source,console]
  341. ----
  342. POST /test*/_search?filter_path=aggregations
  343. {
  344. "aggs": {
  345. "tm": {
  346. "top_metrics": {
  347. "metrics": {"field": "m"},
  348. "sort": {"s": {"order": "asc", "numeric_type": "double"}}
  349. }
  350. }
  351. }
  352. }
  353. ----
  354. // TEST[continued]
  355. Which returns the much more expected:
  356. [source,js]
  357. ----
  358. {
  359. "aggregations": {
  360. "tm": {
  361. "top": [ {"sort": [1.0], "metrics": {"m": 3.1414999961853027 } } ]
  362. }
  363. }
  364. }
  365. ----
  366. // TESTRESPONSE