3
3
[[search-aggregations-metrics-top-metrics]]
4
4
=== Top Metrics Aggregation
5
5
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
-
8
6
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 `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`:
10
8
11
9
[source,console,id=search-aggregations-metrics-top-metrics-simple]
12
10
----
13
11
POST /test/_bulk?refresh
14
12
{"index": {}}
15
- {"s": 1, "v ": 3.1415}
13
+ {"s": 1, "m ": 3.1415}
16
14
{"index": {}}
17
- {"s": 2, "v ": 1.0}
15
+ {"s": 2, "m ": 1.0}
18
16
{"index": {}}
19
- {"s": 3, "v ": 2.71828}
17
+ {"s": 3, "m ": 2.71828}
20
18
POST /test/_search?filter_path=aggregations
21
19
{
22
20
"aggs": {
23
21
"tm": {
24
22
"top_metrics": {
25
- "metrics": {"field": "v "},
23
+ "metrics": {"field": "m "},
26
24
"sort": {"s": "desc"}
27
25
}
28
26
}
@@ -37,7 +35,7 @@ Which returns:
37
35
{
38
36
"aggregations": {
39
37
"tm": {
40
- "top": [ {"sort": [3], "metrics": {"v ": 2.718280076980591 } } ]
38
+ "top": [ {"sort": [3], "metrics": {"m ": 2.718280076980591 } } ]
41
39
}
42
40
}
43
41
}
@@ -54,7 +52,7 @@ The `sort` field in the metric request functions exactly the same as the `sort`
54
52
<<request-body-search-sort, search>> request except:
55
53
* It can't be used on <<binary,binary>>, <<flattened,flattened>, <<ip,ip>>,
56
54
<<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 .
58
56
59
57
The metrics that the aggregation returns is the first hit that would be returned by the search
60
58
request. So,
@@ -65,14 +63,12 @@ request. So,
65
63
gets metrics from the documents with `location` *closest* to `35.7796, -78.6382`
66
64
`"sort": "_score"`:: gets metrics from the document with the highest score
67
65
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
-
71
66
==== `metrics`
72
67
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:
76
72
77
73
[source,console,id=search-aggregations-metrics-top-metrics-list-of-metrics]
78
74
----
@@ -86,19 +82,19 @@ PUT /test
86
82
}
87
83
POST /test/_bulk?refresh
88
84
{"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"}
90
86
{"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"}
92
88
{"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"}
94
90
POST /test/_search?filter_path=aggregations
95
91
{
96
92
"aggs": {
97
93
"tm": {
98
94
"top_metrics": {
99
95
"metrics": [
100
- {"field": "v"},
101
96
{"field": "m"},
97
+ {"field": "i"},
102
98
{"field": "d"}
103
99
],
104
100
"sort": {"s": "desc"}
@@ -118,8 +114,8 @@ Which returns:
118
114
"top": [ {
119
115
"sort": [3],
120
116
"metrics": {
121
- "v ": 2.718280076980591,
122
- "m ": -12,
117
+ "m ": 2.718280076980591,
118
+ "i ": -12,
123
119
"d": "2019-12-31T00:12:12.000Z"
124
120
}
125
121
} ]
@@ -137,19 +133,19 @@ Which returns:
137
133
----
138
134
POST /test/_bulk?refresh
139
135
{"index": {}}
140
- {"s": 1, "v ": 3.1415}
136
+ {"s": 1, "m ": 3.1415}
141
137
{"index": {}}
142
- {"s": 2, "v ": 1.0}
138
+ {"s": 2, "m ": 1.0}
143
139
{"index": {}}
144
- {"s": 3, "v ": 2.71828}
140
+ {"s": 3, "m ": 2.71828}
145
141
POST /test/_search?filter_path=aggregations
146
142
{
147
143
"aggs": {
148
144
"tm": {
149
145
"top_metrics": {
150
- "metrics": {"field": "v "},
146
+ "metrics": {"field": "m "},
151
147
"sort": {"s": "desc"},
152
- "size": 2
148
+ "size": 3
153
149
}
154
150
}
155
151
}
@@ -164,8 +160,9 @@ Which returns:
164
160
"aggregations": {
165
161
"tm": {
166
162
"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 } }
169
166
]
170
167
}
171
168
}
@@ -179,7 +176,8 @@ is a *very* conservative default maximum and you can raise it if you need to by
179
176
changing the `top_metrics_max_size` index setting. But know that large sizes can
180
177
take a fair bit of memory, especially if they are inside of an aggregation which
181
178
makes 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:
183
181
184
182
[source,console]
185
183
----
@@ -190,8 +188,7 @@ PUT /test/_settings
190
188
----
191
189
// TEST[continued]
192
190
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.
195
192
196
193
==== Examples
197
194
@@ -214,11 +211,11 @@ PUT /node
214
211
}
215
212
POST /node/_bulk?refresh
216
213
{"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}
218
215
{"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}
220
217
{"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}
222
219
POST /node/_search?filter_path=aggregations
223
220
{
224
221
"aggs": {
@@ -229,7 +226,7 @@ POST /node/_search?filter_path=aggregations
229
226
"aggs": {
230
227
"tm": {
231
228
"top_metrics": {
232
- "metrics": {"field": "v "},
229
+ "metrics": {"field": "m "},
233
230
"sort": {"date": "desc"}
234
231
}
235
232
}
@@ -251,14 +248,14 @@ Which returns:
251
248
"key": "192.168.0.1",
252
249
"doc_count": 2,
253
250
"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 } } ]
255
252
}
256
253
},
257
254
{
258
255
"key": "192.168.0.2",
259
256
"doc_count": 1,
260
257
"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 } } ]
262
259
}
263
260
}
264
261
],
@@ -280,12 +277,12 @@ POST /node/_search?filter_path=aggregations
280
277
"ip": {
281
278
"terms": {
282
279
"field": "ip",
283
- "order": {"tm.v ": "desc"}
280
+ "order": {"tm.m ": "desc"}
284
281
},
285
282
"aggs": {
286
283
"tm": {
287
284
"top_metrics": {
288
- "metrics": {"field": "v "},
285
+ "metrics": {"field": "m "},
289
286
"sort": {"date": "desc"}
290
287
}
291
288
}
@@ -308,14 +305,14 @@ Which returns:
308
305
"key": "192.168.0.2",
309
306
"doc_count": 1,
310
307
"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 } } ]
312
309
}
313
310
},
314
311
{
315
312
"key": "192.168.0.1",
316
313
"doc_count": 2,
317
314
"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 } } ]
319
316
}
320
317
}
321
318
],
@@ -337,17 +334,17 @@ always sorted independantly of whole numbered fields.
337
334
----
338
335
POST /test/_bulk?refresh
339
336
{"index": {"_index": "test1"}}
340
- {"s": 1, "v ": 3.1415}
337
+ {"s": 1, "m ": 3.1415}
341
338
{"index": {"_index": "test1"}}
342
- {"s": 2, "v ": 1}
339
+ {"s": 2, "m ": 1}
343
340
{"index": {"_index": "test2"}}
344
- {"s": 3.1, "v ": 2.71828}
341
+ {"s": 3.1, "m ": 2.71828}
345
342
POST /test*/_search?filter_path=aggregations
346
343
{
347
344
"aggs": {
348
345
"tm": {
349
346
"top_metrics": {
350
- "metrics": {"field": "v "},
347
+ "metrics": {"field": "m "},
351
348
"sort": {"s": "asc"}
352
349
}
353
350
}
@@ -362,7 +359,7 @@ Which returns:
362
359
{
363
360
"aggregations": {
364
361
"tm": {
365
- "top": [ {"sort": [3.0999999046325684], "metrics": {"v ": 2.718280076980591 } } ]
362
+ "top": [ {"sort": [3.0999999046325684], "metrics": {"m ": 2.718280076980591 } } ]
366
363
}
367
364
}
368
365
}
@@ -380,7 +377,7 @@ POST /test*/_search?filter_path=aggregations
380
377
"aggs": {
381
378
"tm": {
382
379
"top_metrics": {
383
- "metrics": {"field": "v "},
380
+ "metrics": {"field": "m "},
384
381
"sort": {"s": {"order": "asc", "numeric_type": "double"}}
385
382
}
386
383
}
@@ -396,7 +393,7 @@ Which returns the much more expected:
396
393
{
397
394
"aggregations": {
398
395
"tm": {
399
- "top": [ {"sort": [1.0], "metrics": {"v ": 3.1414999961853027 } } ]
396
+ "top": [ {"sort": [1.0], "metrics": {"m ": 3.1414999961853027 } } ]
400
397
}
401
398
}
402
399
}
0 commit comments