cumulative-sum-aggregation.asciidoc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. [[search-aggregations-pipeline-cumulative-sum-aggregation]]
  2. === Cumulative sum aggregation
  3. ++++
  4. <titleabbrev>Cumulative sum</titleabbrev>
  5. ++++
  6. A parent pipeline aggregation which calculates the cumulative sum of a specified metric in a parent histogram (or date_histogram)
  7. aggregation. The specified metric must be numeric and the enclosing histogram must have `min_doc_count` set to `0` (default
  8. for `histogram` aggregations).
  9. ==== Syntax
  10. A `cumulative_sum` aggregation looks like this in isolation:
  11. [source,js]
  12. --------------------------------------------------
  13. {
  14. "cumulative_sum": {
  15. "buckets_path": "the_sum"
  16. }
  17. }
  18. --------------------------------------------------
  19. // NOTCONSOLE
  20. [[cumulative-sum-params]]
  21. .`cumulative_sum` Parameters
  22. [options="header"]
  23. |===
  24. |Parameter Name |Description |Required |Default Value
  25. |`buckets_path` |The path to the buckets we wish to find the cumulative sum for (see <<buckets-path-syntax>> for more
  26. details) |Required |
  27. |`format` |format to apply to the output value of this aggregation |Optional |`null`
  28. |===
  29. The following snippet calculates the cumulative sum of the total monthly `sales`:
  30. [source,console]
  31. --------------------------------------------------
  32. POST /sales/_search
  33. {
  34. "size": 0,
  35. "aggs": {
  36. "sales_per_month": {
  37. "date_histogram": {
  38. "field": "date",
  39. "calendar_interval": "month"
  40. },
  41. "aggs": {
  42. "sales": {
  43. "sum": {
  44. "field": "price"
  45. }
  46. },
  47. "cumulative_sales": {
  48. "cumulative_sum": {
  49. "buckets_path": "sales" <1>
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. --------------------------------------------------
  57. // TEST[setup:sales]
  58. <1> `buckets_path` instructs this cumulative sum aggregation to use the output of the `sales` aggregation for the cumulative sum
  59. And the following may be the response:
  60. [source,console-result]
  61. --------------------------------------------------
  62. {
  63. "took": 11,
  64. "timed_out": false,
  65. "_shards": ...,
  66. "hits": ...,
  67. "aggregations": {
  68. "sales_per_month": {
  69. "buckets": [
  70. {
  71. "key_as_string": "2015/01/01 00:00:00",
  72. "key": 1420070400000,
  73. "doc_count": 3,
  74. "sales": {
  75. "value": 550.0
  76. },
  77. "cumulative_sales": {
  78. "value": 550.0
  79. }
  80. },
  81. {
  82. "key_as_string": "2015/02/01 00:00:00",
  83. "key": 1422748800000,
  84. "doc_count": 2,
  85. "sales": {
  86. "value": 60.0
  87. },
  88. "cumulative_sales": {
  89. "value": 610.0
  90. }
  91. },
  92. {
  93. "key_as_string": "2015/03/01 00:00:00",
  94. "key": 1425168000000,
  95. "doc_count": 2,
  96. "sales": {
  97. "value": 375.0
  98. },
  99. "cumulative_sales": {
  100. "value": 985.0
  101. }
  102. }
  103. ]
  104. }
  105. }
  106. }
  107. --------------------------------------------------
  108. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  109. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  110. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]