1
0

cumulative-sum-aggregation.asciidoc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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` |{javadoc}/java.base/java/text/DecimalFormat.html[DecimalFormat pattern] for the
  28. output value. If specified, the formatted value is returned in the aggregation's
  29. `value_as_string` property |Optional |`null`
  30. |===
  31. The following snippet calculates the cumulative sum of the total monthly `sales`:
  32. [source,console]
  33. --------------------------------------------------
  34. POST /sales/_search
  35. {
  36. "size": 0,
  37. "aggs": {
  38. "sales_per_month": {
  39. "date_histogram": {
  40. "field": "date",
  41. "calendar_interval": "month"
  42. },
  43. "aggs": {
  44. "sales": {
  45. "sum": {
  46. "field": "price"
  47. }
  48. },
  49. "cumulative_sales": {
  50. "cumulative_sum": {
  51. "buckets_path": "sales" <1>
  52. }
  53. }
  54. }
  55. }
  56. }
  57. }
  58. --------------------------------------------------
  59. // TEST[setup:sales]
  60. <1> `buckets_path` instructs this cumulative sum aggregation to use the output of the `sales` aggregation for the cumulative sum
  61. And the following may be the response:
  62. [source,console-result]
  63. --------------------------------------------------
  64. {
  65. "took": 11,
  66. "timed_out": false,
  67. "_shards": ...,
  68. "hits": ...,
  69. "aggregations": {
  70. "sales_per_month": {
  71. "buckets": [
  72. {
  73. "key_as_string": "2015/01/01 00:00:00",
  74. "key": 1420070400000,
  75. "doc_count": 3,
  76. "sales": {
  77. "value": 550.0
  78. },
  79. "cumulative_sales": {
  80. "value": 550.0
  81. }
  82. },
  83. {
  84. "key_as_string": "2015/02/01 00:00:00",
  85. "key": 1422748800000,
  86. "doc_count": 2,
  87. "sales": {
  88. "value": 60.0
  89. },
  90. "cumulative_sales": {
  91. "value": 610.0
  92. }
  93. },
  94. {
  95. "key_as_string": "2015/03/01 00:00:00",
  96. "key": 1425168000000,
  97. "doc_count": 2,
  98. "sales": {
  99. "value": 375.0
  100. },
  101. "cumulative_sales": {
  102. "value": 985.0
  103. }
  104. }
  105. ]
  106. }
  107. }
  108. }
  109. --------------------------------------------------
  110. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  111. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  112. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]