累计求和聚合
编辑累计求和聚合
编辑一个父管道聚合,它计算父直方图(或 date_histogram)聚合中指定指标的累积总和。指定的指标必须是数值型的,并且封闭的直方图必须将 min_doc_count
设置为 0
( histogram
聚合的默认值)。
语法
编辑一个独立的 cumulative_sum
聚合看起来像这样
{ "cumulative_sum": { "buckets_path": "the_sum" } }
表 60. cumulative_sum
参数
参数名称 | 描述 | 必需 | 默认值 |
---|---|---|---|
|
我们要计算累积和的桶的路径(有关详细信息,请参阅 |
必需 |
|
|
输出值的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" } }, "cumulative_sales": { "cumulative_sum": { "buckets_path": "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' } }, cumulative_sales: { cumulative_sum: { buckets_path: '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", }, }, cumulative_sales: { cumulative_sum: { buckets_path: "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" } }, "cumulative_sales": { "cumulative_sum": { "buckets_path": "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 }, "cumulative_sales": { "value": 550.0 } }, { "key_as_string": "2015/02/01 00:00:00", "key": 1422748800000, "doc_count": 2, "sales": { "value": 60.0 }, "cumulative_sales": { "value": 610.0 } }, { "key_as_string": "2015/03/01 00:00:00", "key": 1425168000000, "doc_count": 2, "sales": { "value": 375.0 }, "cumulative_sales": { "value": 985.0 } } ] } } }