管道桶聚合 bucket
语法
Avg Bucket Aggregation
平均值桶聚合
{
"avg_bucket": {
"buckets_path": "the_sum"
}
}
Max Bucket Aggregation
最大值桶聚合
{
"max_bucket": {
"buckets_path": "the_sum"
}
}
Min Bucket Aggregation
最小值桶聚合
{
"min_bucket": {
"buckets_path": "the_sum"
}
}
Sum Bucket Aggregation
总和桶聚合
{
"sum_bucket": {
"buckets_path": "the_sum"
}
}
Stats Bucket Aggregation
统计桶聚合
{
"stats_bucket": {
"buckets_path": "the_sum"
}
}
Extended Stats Bucket Aggregation
扩展统计桶聚合,与stats_bucket聚合相比,该聚合提供了更多的统计信息(平方和、标准偏差等)。
{
"extended_stats_bucket": {
"buckets_path": "the_sum"
}
}
Percentiles Bucket Aggregation
百分比桶聚合
{
"percentiles_bucket": {
"buckets_path": "the_sum"
}
}
xxx_bucket
参数定义
参数名 | 描述 | 是否必须 | 默认值 |
---|---|---|---|
buckets_path | 桶的路径 | 必选 | |
gap_policy | 在数据中发现差距时适用的策略 | 可选 | skip |
format | 应用于此聚合的输出值的格式 | 可选 | null |
示例部分
avg_bucket
以下代码段计算每月总销售额的平均值:
POST /_search
{
"size": 0,
"aggs": {
"sales_per_month": {
"date_histogram": {
"field": "date",
"interval": "month"
},
"aggs": {
"sales": {
"sum": {
"field": "price"
}
}
}
},
"avg_monthly_sales": {
"avg_bucket": {
"buckets_path": "sales_per_month>sales"
}
}
}
}
bucket_path指示这个avg_bucket聚合,我们希望在sales_permonth日期直方图中获得销售聚合的平均值。
返回
{
"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
}
}
]
},
"avg_monthly_sales": {
"value": 328.33333333333333
}
}
}
stats_bucket
以下代码段计算每月销售额的统计数据:
POST /sales/_search
{
"size": 0,
"aggs" : {
"sales_per_month" : {
"date_histogram" : {
"field" : "date",
"interval" : "month"
},
"aggs": {
"sales": {
"sum": {
"field": "price"
}
}
}
},
"stats_monthly_sales": {
"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
}
}
}
extended_stats_bucket
下面的代码段计算了每月销售额桶的扩展统计数据:
POST /sales/_search
{
"size": 0,
"aggs" : {
"sales_per_month" : {
"date_histogram" : {
"field" : "date",
"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,
"std_deviation": 202.74505063146563,
"std_deviation_bounds": {
"upper": 733.8234345962646,
"lower": -77.15676792959795
}
}
}
}
percentiles_bucket
以下代码段计算每月总销售时段的百分比:
POST /sales/_search
{
"size": 0,
"aggs" : {
"sales_per_month" : {
"date_histogram" : {
"field" : "date",
"interval" : "month"
},
"aggs": {
"sales": {
"sum": {
"field": "price"
}
}
}
},
"percentiles_monthly_sales": {
"percentiles_bucket": {
"buckets_path": "sales_per_month>sales",
"percents": [ 25.0, 50.0, 75.0 ]
}
}
}
}