33[[search-aggregations-metrics-top-metrics]]
44=== Top Metrics Aggregation
55
6- experimental[We expect to change the response format of this aggregation as we add more features., https://github.com/elastic/elasticsearch/issues/51813]
7-
86The `top_metrics` aggregation selects metrics from the document with the largest or smallest "sort"
9- value. For example, This gets the value of the `v ` field on the document with the largest value of `s`:
7+ value. For example, this gets the value of the `m ` field on the document with the largest value of `s`:
108
119[source,console,id=search-aggregations-metrics-top-metrics-simple]
1210----
1311POST /test/_bulk?refresh
1412{"index": {}}
15- {"s": 1, "v ": 3.1415}
13+ {"s": 1, "m ": 3.1415}
1614{"index": {}}
17- {"s": 2, "v ": 1.0}
15+ {"s": 2, "m ": 1.0}
1816{"index": {}}
19- {"s": 3, "v ": 2.71828}
17+ {"s": 3, "m ": 2.71828}
2018POST /test/_search?filter_path=aggregations
2119{
2220 "aggs": {
2321 "tm": {
2422 "top_metrics": {
25- "metrics": {"field": "v "},
23+ "metrics": {"field": "m "},
2624 "sort": {"s": "desc"}
2725 }
2826 }
@@ -37,7 +35,7 @@ Which returns:
3735{
3836 "aggregations": {
3937 "tm": {
40- "top": [ {"sort": [3], "metrics": {"v ": 2.718280076980591 } } ]
38+ "top": [ {"sort": [3], "metrics": {"m ": 2.718280076980591 } } ]
4139 }
4240 }
4341}
@@ -54,7 +52,7 @@ The `sort` field in the metric request functions exactly the same as the `sort`
5452<<request-body-search-sort, search>> request except:
5553* It can't be used on <<binary,binary>>, <<flattened,flattened>, <<ip,ip>>,
5654<<keyword,keyword>>, or <<text,text>> fields.
57- * It only supports a single sort value.
55+ * It only supports a single sort value so which document wins ties is not specified .
5856
5957The metrics that the aggregation returns is the first hit that would be returned by the search
6058request. So,
@@ -65,14 +63,12 @@ request. So,
6563 gets metrics from the documents with `location` *closest* to `35.7796, -78.6382`
6664`"sort": "_score"`:: gets metrics from the document with the highest score
6765
68- NOTE: This aggregation doesn't support any sort of "tie breaking". If two documents have
69- the same sort values then this aggregation could return either document's fields.
70-
7166==== `metrics`
7267
73- `metrics` selects the fields to of the "top" document to return.
74-
75- You can return multiple metrics by providing a list:
68+ `metrics` selects the fields to of the "top" document to return. You can return
69+ a request a single metric with something like `"metric": {"field": "m"}` or multiple
70+ metrics by requesting a list of metrics like `"metric": [{"field": "m"}, {"field": "i"}`.
71+ Here is a more complete example:
7672
7773[source,console,id=search-aggregations-metrics-top-metrics-list-of-metrics]
7874----
@@ -86,19 +82,19 @@ PUT /test
8682}
8783POST /test/_bulk?refresh
8884{"index": {}}
89- {"s": 1, "v ": 3.1415, "m ": 1, "d": "2020-01-01T00:12:12Z"}
85+ {"s": 1, "m ": 3.1415, "i ": 1, "d": "2020-01-01T00:12:12Z"}
9086{"index": {}}
91- {"s": 2, "v ": 1.0, "m ": 6, "d": "2020-01-02T00:12:12Z"}
87+ {"s": 2, "m ": 1.0, "i ": 6, "d": "2020-01-02T00:12:12Z"}
9288{"index": {}}
93- {"s": 3, "v ": 2.71828, "m ": -12, "d": "2019-12-31T00:12:12Z"}
89+ {"s": 3, "m ": 2.71828, "i ": -12, "d": "2019-12-31T00:12:12Z"}
9490POST /test/_search?filter_path=aggregations
9591{
9692 "aggs": {
9793 "tm": {
9894 "top_metrics": {
9995 "metrics": [
100- {"field": "v"},
10196 {"field": "m"},
97+ {"field": "i"},
10298 {"field": "d"}
10399 ],
104100 "sort": {"s": "desc"}
@@ -118,8 +114,8 @@ Which returns:
118114 "top": [ {
119115 "sort": [3],
120116 "metrics": {
121- "v ": 2.718280076980591,
122- "m ": -12,
117+ "m ": 2.718280076980591,
118+ "i ": -12,
123119 "d": "2019-12-31T00:12:12.000Z"
124120 }
125121 } ]
@@ -137,19 +133,19 @@ Which returns:
137133----
138134POST /test/_bulk?refresh
139135{"index": {}}
140- {"s": 1, "v ": 3.1415}
136+ {"s": 1, "m ": 3.1415}
141137{"index": {}}
142- {"s": 2, "v ": 1.0}
138+ {"s": 2, "m ": 1.0}
143139{"index": {}}
144- {"s": 3, "v ": 2.71828}
140+ {"s": 3, "m ": 2.71828}
145141POST /test/_search?filter_path=aggregations
146142{
147143 "aggs": {
148144 "tm": {
149145 "top_metrics": {
150- "metrics": {"field": "v "},
146+ "metrics": {"field": "m "},
151147 "sort": {"s": "desc"},
152- "size": 2
148+ "size": 3
153149 }
154150 }
155151 }
@@ -164,8 +160,9 @@ Which returns:
164160 "aggregations": {
165161 "tm": {
166162 "top": [
167- {"sort": [3], "metrics": {"v": 2.718280076980591 } },
168- {"sort": [2], "metrics": {"v": 1.0 } }
163+ {"sort": [3], "metrics": {"m": 2.718280076980591 } },
164+ {"sort": [2], "metrics": {"m": 1.0 } },
165+ {"sort": [1], "metrics": {"m": 3.1414999961853027 } }
169166 ]
170167 }
171168 }
@@ -179,7 +176,8 @@ is a *very* conservative default maximum and you can raise it if you need to by
179176changing the `top_metrics_max_size` index setting. But know that large sizes can
180177take a fair bit of memory, especially if they are inside of an aggregation which
181178makes many buckes like a large
182- <<search-aggregations-metrics-top-metrics-example-terms, terms aggregation>>.
179+ <<search-aggregations-metrics-top-metrics-example-terms, terms aggregation>>. If
180+ you till want to raise it, use something like:
183181
184182[source,console]
185183----
@@ -190,8 +188,7 @@ PUT /test/_settings
190188----
191189// TEST[continued]
192190
193- NOTE: If `size` is more than `1` the `top_metrics` aggregation can't be the target of a sort.
194-
191+ NOTE: If `size` is more than `1` the `top_metrics` aggregation can't be the *target* of a sort.
195192
196193==== Examples
197194
@@ -214,11 +211,11 @@ PUT /node
214211}
215212POST /node/_bulk?refresh
216213{"index": {}}
217- {"ip": "192.168.0.1", "date": "2020-01-01T01:01:01", "v ": 1}
214+ {"ip": "192.168.0.1", "date": "2020-01-01T01:01:01", "m ": 1}
218215{"index": {}}
219- {"ip": "192.168.0.1", "date": "2020-01-01T02:01:01", "v ": 2}
216+ {"ip": "192.168.0.1", "date": "2020-01-01T02:01:01", "m ": 2}
220217{"index": {}}
221- {"ip": "192.168.0.2", "date": "2020-01-01T02:01:01", "v ": 3}
218+ {"ip": "192.168.0.2", "date": "2020-01-01T02:01:01", "m ": 3}
222219POST /node/_search?filter_path=aggregations
223220{
224221 "aggs": {
@@ -229,7 +226,7 @@ POST /node/_search?filter_path=aggregations
229226 "aggs": {
230227 "tm": {
231228 "top_metrics": {
232- "metrics": {"field": "v "},
229+ "metrics": {"field": "m "},
233230 "sort": {"date": "desc"}
234231 }
235232 }
@@ -251,14 +248,14 @@ Which returns:
251248 "key": "192.168.0.1",
252249 "doc_count": 2,
253250 "tm": {
254- "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"v ": 2 } } ]
251+ "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"m ": 2 } } ]
255252 }
256253 },
257254 {
258255 "key": "192.168.0.2",
259256 "doc_count": 1,
260257 "tm": {
261- "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"v ": 3 } } ]
258+ "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"m ": 3 } } ]
262259 }
263260 }
264261 ],
@@ -280,12 +277,12 @@ POST /node/_search?filter_path=aggregations
280277 "ip": {
281278 "terms": {
282279 "field": "ip",
283- "order": {"tm.v ": "desc"}
280+ "order": {"tm.m ": "desc"}
284281 },
285282 "aggs": {
286283 "tm": {
287284 "top_metrics": {
288- "metrics": {"field": "v "},
285+ "metrics": {"field": "m "},
289286 "sort": {"date": "desc"}
290287 }
291288 }
@@ -308,14 +305,14 @@ Which returns:
308305 "key": "192.168.0.2",
309306 "doc_count": 1,
310307 "tm": {
311- "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"v ": 3 } } ]
308+ "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"m ": 3 } } ]
312309 }
313310 },
314311 {
315312 "key": "192.168.0.1",
316313 "doc_count": 2,
317314 "tm": {
318- "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"v ": 2 } } ]
315+ "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"m ": 2 } } ]
319316 }
320317 }
321318 ],
@@ -337,17 +334,17 @@ always sorted independantly of whole numbered fields.
337334----
338335POST /test/_bulk?refresh
339336{"index": {"_index": "test1"}}
340- {"s": 1, "v ": 3.1415}
337+ {"s": 1, "m ": 3.1415}
341338{"index": {"_index": "test1"}}
342- {"s": 2, "v ": 1}
339+ {"s": 2, "m ": 1}
343340{"index": {"_index": "test2"}}
344- {"s": 3.1, "v ": 2.71828}
341+ {"s": 3.1, "m ": 2.71828}
345342POST /test*/_search?filter_path=aggregations
346343{
347344 "aggs": {
348345 "tm": {
349346 "top_metrics": {
350- "metrics": {"field": "v "},
347+ "metrics": {"field": "m "},
351348 "sort": {"s": "asc"}
352349 }
353350 }
@@ -362,7 +359,7 @@ Which returns:
362359{
363360 "aggregations": {
364361 "tm": {
365- "top": [ {"sort": [3.0999999046325684], "metrics": {"v ": 2.718280076980591 } } ]
362+ "top": [ {"sort": [3.0999999046325684], "metrics": {"m ": 2.718280076980591 } } ]
366363 }
367364 }
368365}
@@ -380,7 +377,7 @@ POST /test*/_search?filter_path=aggregations
380377 "aggs": {
381378 "tm": {
382379 "top_metrics": {
383- "metrics": {"field": "v "},
380+ "metrics": {"field": "m "},
384381 "sort": {"s": {"order": "asc", "numeric_type": "double"}}
385382 }
386383 }
@@ -396,7 +393,7 @@ Which returns the much more expected:
396393{
397394 "aggregations": {
398395 "tm": {
399- "top": [ {"sort": [1.0], "metrics": {"v ": 3.1414999961853027 } } ]
396+ "top": [ {"sort": [1.0], "metrics": {"m ": 3.1414999961853027 } } ]
400397 }
401398 }
402399}
0 commit comments