cumulative-sum-aggregation.asciidoc 3.3 KB

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