Aggregate metric field type | Elasticsearch Guide [7.17] | Elastic
对于object类型的字段来说,可以存子字段为 min/max/sum/value_count
PUT my-index {"mappings": {"properties": {"my-agg-metric-field": { -- 字段名"type": "aggregate_metric_double", --字段类型"metrics": [ "min", "max", "sum", "value_count" ], --那些聚合操作"default_metric": "max" -- 默认显示的聚合字段 用来query的}}} }
Parameters for aggregate_metric_double
fields
metrics
(Required, array of strings) Array of metric sub-fields to store. Each value corresponds to a metric aggregation. Valid values are min, max, sum, and value_count. You must specify at least one value.
--只能是min/max/sum/value_count
default_metric
(Required, string) Default metric sub-field to use for queries, scripts, and aggregations that don’t use a sub-field. Must be a value from the
metrics
array.-- 必须是metrics里的
time_series_metric
[preview] This functionality is in technical preview and may be changed or removed in a future release. Elastic will work to fix any issues, but features in technical preview are not subject to the support SLA of official GA features. (Optional, string)
For internal use by Elastic only.
--还没开放。
Uses
We designed
aggregate_metric_double
fields for use with the following aggregations:
- A min aggregation returns the minimum value of all
min
sub-fields.- A max aggregation returns the maximum value of all
max
sub-fields.- A sum aggregation returns the sum of the values of all
sum
sub-fields.- A value_count aggregation returns the sum of the values of all
value_count
sub-fields.- A avg aggregation. There is no
avg
sub-field; the result of theavg
aggregation is computed using thesum
andvalue_count
metrics. To run anavg
aggregation, the field must contain bothsum
andvalue_count
metric sub-field.-- 这里的sum和value_count都是 sum,但是value_count的用处是来计算avg
Running any other aggregation on an
aggregate_metric_double
field will fail with an "unsupported aggregation" error.Finally, an
aggregate_metric_double
field supports the following queries for which it behaves as adouble
by delegating its behavior to itsdefault_metric
sub-field:
- exists
- range
- term
- terms
-- 目前type=
aggregate_metric_double 只持支持min/max/sum/value_count,其他类型会报错,目前也只支持下面4种查询
实战开始。
PUT stats-index
{
"mappings": {
"properties": {
"agg_metric": {
"type": "aggregate_metric_double",
"metrics": [ "min", "max", "sum", "value_count" ],
"default_metric": "max"
}
}
}
}PUT stats-index/_doc/1
{
"agg_metric": {
"min": -302.50,
"max": 702.30,
"sum": 200.0,
"value_count": 25
}
}PUT stats-index/_doc/2
{
"agg_metric": {
"min": -93.00,
"max": 1702.30,
"sum": 300.00,
"value_count": 25
}
}
查询数据
POST stats-index/_search?size=0
{
"aggs": {
"metric_min": { "min": { "field": "agg_metric" } },
"metric_max": { "max": { "field": "agg_metric" } },
"metric_value_count": { "value_count": { "field": "agg_metric" } },
"metric_sum": { "sum": { "field": "agg_metric" } },
"metric_avg": { "avg": { "field": "agg_metric" } }
}
}-- 说下这个查询
-- size=0 意思是不看基础数据,只看聚合后的结果数据
-- aggs 是类似 term must 代表现在查的聚合数据
-- metric_min 代表聚合后的字段名,
--min 代表是哪个聚合方式min/max。。。
--"field": "agg_metric" 代表的是对哪个字段进行聚合可能有agg_metric1,agg_metric2
查询基础数据
GET stats-index/_search
{
"query": {
"term": {
"agg_metric": {
"value": 1702.30
}
}
}
}注意这里查询的是 max的值 也就是上文提到的default_metric