cumulative-sum-aggregation.asciidoc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. [[search-aggregations-pipeline-cumulative-sum-aggregation]]
  2. === Cumulative Sum Aggregation
  3. experimental[]
  4. A parent pipeline aggregation which calculates the cumulative sum of a specified metric in a parent histogram (or date_histogram)
  5. aggregation. The specified metric must be numeric and the enclosing histogram must have `min_doc_count` set to `0` (default
  6. for `histogram` aggregations).
  7. ==== Syntax
  8. A `cumulative_sum` aggregation looks like this in isolation:
  9. [source,js]
  10. --------------------------------------------------
  11. {
  12. "cumulative_sum": {
  13. "buckets_path": "the_sum"
  14. }
  15. }
  16. --------------------------------------------------
  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, defaults to `null` |
  23. |===
  24. The following snippet calculates the cumulative sum of the total monthly `sales`:
  25. [source,js]
  26. --------------------------------------------------
  27. {
  28. "aggs" : {
  29. "sales_per_month" : {
  30. "date_histogram" : {
  31. "field" : "date",
  32. "interval" : "month"
  33. },
  34. "aggs": {
  35. "sales": {
  36. "sum": {
  37. "field": "price"
  38. }
  39. },
  40. "cumulative_sales": {
  41. "cumulative_sum": {
  42. "buckets_path": "sales" <1>
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49. --------------------------------------------------
  50. <1> `buckets_path` instructs this cumulative sum aggregation to use the output of the `sales` aggregation for the cumulative sum
  51. And the following may be the response:
  52. [source,js]
  53. --------------------------------------------------
  54. {
  55. "aggregations": {
  56. "sales_per_month": {
  57. "buckets": [
  58. {
  59. "key_as_string": "2015/01/01 00:00:00",
  60. "key": 1420070400000,
  61. "doc_count": 3,
  62. "sales": {
  63. "value": 550
  64. },
  65. "cumulative_sales": {
  66. "value": 550
  67. }
  68. },
  69. {
  70. "key_as_string": "2015/02/01 00:00:00",
  71. "key": 1422748800000,
  72. "doc_count": 2,
  73. "sales": {
  74. "value": 60
  75. },
  76. "cumulative_sales": {
  77. "value": 610
  78. }
  79. },
  80. {
  81. "key_as_string": "2015/03/01 00:00:00",
  82. "key": 1425168000000,
  83. "doc_count": 2,
  84. "sales": {
  85. "value": 375
  86. },
  87. "cumulative_sales": {
  88. "value": 985
  89. }
  90. }
  91. ]
  92. }
  93. }
  94. }
  95. --------------------------------------------------