top-metrics-aggregation.asciidoc 8.7 KB

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