导数聚合
编辑导数聚合
编辑一个父级管道聚合,用于计算父级直方图(或 date_histogram)聚合中指定指标的导数。指定的指标必须是数字,并且封闭的直方图的 min_doc_count
必须设置为 0
(histogram
聚合的默认值)。
语法
编辑一个 derivative
聚合单独使用时看起来像这样
"derivative": { "buckets_path": "the_sum" }
表 61. derivative
参数
参数名称 | 描述 | 必需 | 默认值 |
---|---|---|---|
|
我们希望查找导数的桶的路径(有关详细信息,请参阅 |
必需 |
|
|
在数据中发现间隙时应用的策略(有关详细信息,请参阅 处理数据中的间隙) |
可选 |
|
|
用于输出值的 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" } }, "sales_deriv": { "derivative": { "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' } }, sales_deriv: { derivative: { 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", }, }, sales_deriv: { derivative: { 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" } }, "sales_deriv": { "derivative": { "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 } }, { "key_as_string": "2015/02/01 00:00:00", "key": 1422748800000, "doc_count": 2, "sales": { "value": 60.0 }, "sales_deriv": { "value": -490.0 } }, { "key_as_string": "2015/03/01 00:00:00", "key": 1425168000000, "doc_count": 2, "sales": { "value": 375.0 }, "sales_deriv": { "value": 315.0 } } ] } } }
二阶导数
编辑可以通过将导数管道聚合链接到另一个导数管道聚合的结果来计算二阶导数,如下例所示,它将计算每月总销售额的一阶导数和二阶导数
resp = client.search( index="sales", size=0, aggs={ "sales_per_month": { "date_histogram": { "field": "date", "calendar_interval": "month" }, "aggs": { "sales": { "sum": { "field": "price" } }, "sales_deriv": { "derivative": { "buckets_path": "sales" } }, "sales_2nd_deriv": { "derivative": { "buckets_path": "sales_deriv" } } } } }, ) 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' } }, sales_deriv: { derivative: { buckets_path: 'sales' } }, "sales_2nd_deriv": { derivative: { buckets_path: 'sales_deriv' } } } } } } ) 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", }, }, sales_deriv: { derivative: { buckets_path: "sales", }, }, sales_2nd_deriv: { derivative: { buckets_path: "sales_deriv", }, }, }, }, }, }); console.log(response);
POST /sales/_search { "size": 0, "aggs": { "sales_per_month": { "date_histogram": { "field": "date", "calendar_interval": "month" }, "aggs": { "sales": { "sum": { "field": "price" } }, "sales_deriv": { "derivative": { "buckets_path": "sales" } }, "sales_2nd_deriv": { "derivative": { "buckets_path": "sales_deriv" } } } } } }
以下可能是响应
{ "took": 50, "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 }, "sales_deriv": { "value": -490.0 } }, { "key_as_string": "2015/03/01 00:00:00", "key": 1425168000000, "doc_count": 2, "sales": { "value": 375.0 }, "sales_deriv": { "value": 315.0 }, "sales_2nd_deriv": { "value": 805.0 } } ] } } }
单位
编辑导数聚合允许指定导数值的单位。这会在响应中返回一个额外的字段 normalized_value
,它以所需的 x 轴单位报告导数值。在下面的示例中,我们计算每月总销售额的导数,但要求销售额的导数以每天的销售额为单位
resp = client.search( index="sales", size=0, aggs={ "sales_per_month": { "date_histogram": { "field": "date", "calendar_interval": "month" }, "aggs": { "sales": { "sum": { "field": "price" } }, "sales_deriv": { "derivative": { "buckets_path": "sales", "unit": "day" } } } } }, ) 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' } }, sales_deriv: { derivative: { buckets_path: 'sales', unit: 'day' } } } } } } ) 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", }, }, sales_deriv: { derivative: { buckets_path: "sales", unit: "day", }, }, }, }, }, }); console.log(response);
POST /sales/_search { "size": 0, "aggs": { "sales_per_month": { "date_histogram": { "field": "date", "calendar_interval": "month" }, "aggs": { "sales": { "sum": { "field": "price" } }, "sales_deriv": { "derivative": { "buckets_path": "sales", "unit": "day" } } } } } }
以下可能是响应
{ "took": 50, "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 }, "sales_deriv": { "value": -490.0, "normalized_value": -15.806451612903226 } }, { "key_as_string": "2015/03/01 00:00:00", "key": 1425168000000, "doc_count": 2, "sales": { "value": 375.0 }, "sales_deriv": { "value": 315.0, "normalized_value": 11.25 } } ] } } }