最小桶聚合
编辑最小桶聚合
编辑一个同级管道聚合,用于识别同级聚合中指定指标的最小值所在的桶,并输出桶的值和键。指定的指标必须是数值型,且同级聚合必须是多桶聚合。
语法
编辑一个 min_bucket
聚合的单独形式如下所示:
{ "min_bucket": { "buckets_path": "the_sum" } }
表 65. min_bucket
参数
参数名称 | 描述 | 必需 | 默认值 |
---|---|---|---|
|
要查找最小值的桶的路径(有关详细信息,请参阅 |
必需 |
|
|
在数据中发现缺口时应用的策略(有关详细信息,请参阅 处理数据中的缺口) |
可选 |
|
|
输出值的 DecimalFormat 模式。如果指定,格式化的值将返回到聚合的 |
可选 |
|
以下代码片段计算每月总 sales
的最小值
resp = client.search( index="sales", size=0, aggs={ "sales_per_month": { "date_histogram": { "field": "date", "calendar_interval": "month" }, "aggs": { "sales": { "sum": { "field": "price" } } } }, "min_monthly_sales": { "min_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' } } } }, min_monthly_sales: { min_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", }, }, }, }, min_monthly_sales: { min_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" } } } }, "min_monthly_sales": { "min_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 } } ] }, "min_monthly_sales": { "keys": ["2015/02/01 00:00:00"], "value": 60.0 } } }