Cumulative Sum Aggregation(累积汇总聚合)

警告

此功能是实验性的,可能会在未来的版本中完全更改或删除。Elastic将采取最大的努力来解决任何的问题,但是实验功能不受SLA官方功能的支持。

父级管道聚合,计算父级直方图(或日期直方图)聚合中指定度量的累积和。指定的度量必须是数字,并且闭合min_doc_count直方图必须设置为0(默认为histogram聚合)。

Syntax(语法)

cumulative_sum聚合看起来像这样:

  1. {
  2. "cumulative_sum": {
  3. "buckets_path": "the_sum"
  4. }
  5. }

Table 10. cumulative_sum Parameters(Table 10.cumulative_sum参数)

参数名称 描述 是否必须 默认值
buckets_path 我们希望找到的数据的桶的路径(相关详细信息,请参阅“buckets_path Syntax”一节) 必须
format 应用于此聚合的输出值的格式 可选 null

以下代码计算每月sales总额的累计总和:

  1. POST /sales/_search
  2. {
  3. "size": 0,
  4. "aggs" : {
  5. "sales_per_month" : {
  6. "date_histogram" : {
  7. "field" : "date",
  8. "interval" : "month"
  9. },
  10. "aggs": {
  11. "sales": {
  12. "sum": {
  13. "field": "price"
  14. }
  15. },
  16. "cumulative_sales": {
  17. "cumulative_sum": {
  18. "buckets_path": "sales" #1
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }

| 1 | buckets_path指示这个为cumulatice sum聚合,以使用累积和来输出sales聚合。 |

以下是响应信息:

  1. {
  2. "took": 11,
  3. "timed_out": false,
  4. "_shards": ...,
  5. "hits": ...,
  6. "aggregations": {
  7. "sales_per_month": {
  8. "buckets": [
  9. {
  10. "key_as_string": "2015/01/01 00:00:00",
  11. "key": 1420070400000,
  12. "doc_count": 3,
  13. "sales": {
  14. "value": 550.0
  15. },
  16. "cumulative_sales": {
  17. "value": 550.0
  18. }
  19. },
  20. {
  21. "key_as_string": "2015/02/01 00:00:00",
  22. "key": 1422748800000,
  23. "doc_count": 2,
  24. "sales": {
  25. "value": 60.0
  26. },
  27. "cumulative_sales": {
  28. "value": 610.0
  29. }
  30. },
  31. {
  32. "key_as_string": "2015/03/01 00:00:00",
  33. "key": 1425168000000,
  34. "doc_count": 2,
  35. "sales": {
  36. "value": 375.0
  37. },
  38. "cumulative_sales": {
  39. "value": 985.0
  40. }
  41. }
  42. ]
  43. }
  44. }
  45. }

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-cumulative-sum-aggregation.html(修改该链接为官网对应的链接)

译文链接 : http://www.apache.wiki/pages/viewpage.action?pageId=10030390(修改该链接为 ApacheCN 对应的译文链接)

贡献者 : 曾少峰ApacheCNApache中文网