桶选择器聚合
编辑桶选择器聚合
编辑一个父管道聚合,它执行一个脚本,该脚本确定当前桶是否将保留在父多桶聚合中。指定的指标必须是数字,并且脚本必须返回布尔值。如果脚本语言是 expression
,则允许返回数字值。在这种情况下,0.0 将被评估为 false
,所有其他值都将评估为 true。
bucket_selector 聚合与所有管道聚合一样,在所有其他兄弟聚合之后执行。这意味着使用 bucket_selector 聚合来过滤响应中返回的桶不会节省运行聚合的执行时间。
语法
编辑一个 bucket_selector
聚合在孤立状态下如下所示
{ "bucket_selector": { "buckets_path": { "my_var1": "the_sum", "my_var2": "the_value_count" }, "script": "params.my_var1 > params.my_var2" } }
表 58. bucket_selector
参数
参数名称 | 描述 | 必填 | 默认值 |
---|---|---|---|
|
为此聚合运行的脚本。脚本可以是内联的、文件的或索引的。(有关更多详细信息,请参阅 脚本) |
必填 |
|
|
脚本变量与其关联路径到我们希望用于变量的桶的映射(有关更多详细信息,请参阅 |
必填 |
|
|
在数据中找到间隙时要应用的策略(有关更多详细信息,请参阅 处理数据中的间隙) |
可选 |
|
以下代码段仅保留月总销售额超过 200 的桶
resp = client.search( index="sales", size=0, aggs={ "sales_per_month": { "date_histogram": { "field": "date", "calendar_interval": "month" }, "aggs": { "total_sales": { "sum": { "field": "price" } }, "sales_bucket_filter": { "bucket_selector": { "buckets_path": { "totalSales": "total_sales" }, "script": "params.totalSales > 200" } } } } }, ) print(resp)
response = client.search( index: 'sales', body: { size: 0, aggregations: { sales_per_month: { date_histogram: { field: 'date', calendar_interval: 'month' }, aggregations: { total_sales: { sum: { field: 'price' } }, sales_bucket_filter: { bucket_selector: { buckets_path: { "totalSales": 'total_sales' }, script: 'params.totalSales > 200' } } } } } } ) puts response
const response = await client.search({ index: "sales", size: 0, aggs: { sales_per_month: { date_histogram: { field: "date", calendar_interval: "month", }, aggs: { total_sales: { sum: { field: "price", }, }, sales_bucket_filter: { bucket_selector: { buckets_path: { totalSales: "total_sales", }, script: "params.totalSales > 200", }, }, }, }, }, }); console.log(response);
POST /sales/_search { "size": 0, "aggs": { "sales_per_month": { "date_histogram": { "field": "date", "calendar_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 } } ] } } }