扩展统计桶聚合
编辑扩展统计桶聚合
编辑一个兄弟管道聚合,用于计算指定兄弟聚合中指定指标的所有桶的各种统计信息。指定的指标必须是数值,并且兄弟聚合必须是多桶聚合。
与 stats_bucket
聚合相比,此聚合提供了更多统计信息(平方和、标准差等)。
语法
编辑一个 extended_stats_bucket
聚合在隔离状态下如下所示
{ "extended_stats_bucket": { "buckets_path": "the_sum" } }
表 62. extended_stats_bucket
参数
参数名称 | 描述 | 是否必需 | 默认值 |
---|---|---|---|
|
我们要计算统计信息的桶的路径(有关更多详细信息,请参见 |
是否必需 |
|
|
在数据中发现缺口时应用的策略(有关更多详细信息,请参见 处理数据中的缺口) |
可选 |
|
|
输出值的 DecimalFormat 模式。如果指定,格式化的值将返回到聚合的 |
可选 |
|
|
要显示的平均值之上/之下的标准偏差数 |
可选 |
2 |
以下代码片段计算每月 sales
桶的扩展统计信息
resp = client.search( index="sales", size=0, aggs={ "sales_per_month": { "date_histogram": { "field": "date", "calendar_interval": "month" }, "aggs": { "sales": { "sum": { "field": "price" } } } }, "stats_monthly_sales": { "extended_stats_bucket": { "buckets_path": "sales_per_month>sales" } } }, ) print(resp)
response = client.search( index: 'sales', body: { size: 0, aggregations: { sales_per_month: { date_histogram: { field: 'date', calendar_interval: 'month' }, aggregations: { sales: { sum: { field: 'price' } } } }, stats_monthly_sales: { extended_stats_bucket: { buckets_path: 'sales_per_month>sales' } } } } ) puts response
const response = await client.search({ index: "sales", size: 0, aggs: { sales_per_month: { date_histogram: { field: "date", calendar_interval: "month", }, aggs: { sales: { sum: { field: "price", }, }, }, }, stats_monthly_sales: { extended_stats_bucket: { buckets_path: "sales_per_month>sales", }, }, }, }); console.log(response);
POST /sales/_search { "size": 0, "aggs": { "sales_per_month": { "date_histogram": { "field": "date", "calendar_interval": "month" }, "aggs": { "sales": { "sum": { "field": "price" } } } }, "stats_monthly_sales": { "extended_stats_bucket": { "buckets_path": "sales_per_month>sales" } } } }
以下可能是响应
{ "took": 11, "timed_out": false, "_shards": ..., "hits": ..., "aggregations": { "sales_per_month": { "buckets": [ { "key_as_string": "2015/01/01 00:00:00", "key": 1420070400000, "doc_count": 3, "sales": { "value": 550.0 } }, { "key_as_string": "2015/02/01 00:00:00", "key": 1422748800000, "doc_count": 2, "sales": { "value": 60.0 } }, { "key_as_string": "2015/03/01 00:00:00", "key": 1425168000000, "doc_count": 2, "sales": { "value": 375.0 } } ] }, "stats_monthly_sales": { "count": 3, "min": 60.0, "max": 550.0, "avg": 328.3333333333333, "sum": 985.0, "sum_of_squares": 446725.0, "variance": 41105.55555555556, "variance_population": 41105.55555555556, "variance_sampling": 61658.33333333334, "std_deviation": 202.74505063146563, "std_deviation_population": 202.74505063146563, "std_deviation_sampling": 248.3109609609156, "std_deviation_bounds": { "upper": 733.8234345962646, "lower": -77.15676792959795, "upper_population" : 733.8234345962646, "lower_population" : -77.15676792959795, "upper_sampling" : 824.9552552551645, "lower_sampling" : -168.28858858849787 } } } }