跳到主要内容

管道桶选择器 bucket_selector

Bucket Selector Aggregation

属于parent管道聚合,它执行一个脚本,该脚本确定当前桶是否将保留在父多桶聚合中。指定的度量必须是数字,并且脚本必须返回布尔值。如果脚本语言是表达式,则允许数字返回值。在这种情况下,0.0将被计算为false,所有其他值将被计算成true

语法

bucket_selector aggregation

{
"bucket_selector": {
"buckets_path": {
"my_var1": "the_sum",
"my_var2": "the_value_count"
},
"script": "params.my_var1 > params.my_var2"
}
}

bucket_selector 参数

参数说明缺省默认值
script要为此聚合运行的脚本必填
buckets_path脚本变量及其关联路径到我们希望用于变量的Bucket的映射必填
gap_policy在数据中发现差距时应用的策略可选skip

以下片段仅保留当月总销售额超过200的桶:

POST /sales/_search
{
"size": 0,
"aggs" : {
"sales_per_month" : {
"date_histogram" : {
"field" : "date",
"interval" : "month"
},
"aggs": {
"total_sales": {
"sum": {
"field": "price"
}
},
"sales_bucket_filter": {
"bucket_selector": {
"buckets_path": {
"totalSales": "total_sales"
},
"script": "params.totalSales > 200"
}
}
}
}
}
}

返回

{
"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,
"total_sales": {
"value": 550.0
}
},
{
"key_as_string": "2015/03/01 00:00:00",
"key": 1425168000000,
"doc_count": 2,
"total_sales": {
"value": 375.0
},
}
]
}
}
}