原英文版地址: https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-pipeline-derivative-aggregation.html, 原文档版权归 www.elastic.co 所有
本地英文版地址: ../en/search-aggregations-pipeline-derivative-aggregation.html
本地英文版地址: ../en/search-aggregations-pipeline-derivative-aggregation.html
重要: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 当前版本文档。
一种父管道聚合,用于计算父直方图(或日期直方图date_histogram)聚合中指定度量的导数。
指定的度量必须是数字,并且封闭直方图的min_doc_count
必须设置为0
(histogram
聚合的默认值)。
一个单独的derivative
看起来像这样:
"derivative": { "buckets_path": "the_sum" }
表 16. derivative
参数
参数名称 | 描述 | 是否必需 | 默认值 |
---|---|---|---|
|
我们希望找到其导数的桶的路径(更多详情请参考 |
必需 |
|
|
在数据中发现间隙时应用的策略(更多详情请参考处理数据中的间隙) |
可选 |
|
|
应用于此聚合的输出值的格式 |
可选 |
|
下面这个代码片段计算每月总销售额(sales
)的导数:
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 } } ] } } }
可以通过将导数管道聚合链接到另一个导数管道聚合的结果来计算二阶导数,如下例所示,该例将计算每月总销售额(sales)的一阶导数和二阶导数:
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轴单位中的导数值。
在下面的示例中,我们计算每月总销售额(sales)的导数,但要求销售额的导数以每天的销售额为单位:
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 } } ] } } }