cumulative-sum-aggregation.asciidoc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. .`cumulative_sum` Parameters
  17. |===
  18. |Parameter Name |Description |Required |Default Value
  19. |`buckets_path` |The path to the buckets we wish to find the cumulative sum for (see <<bucket-path-syntax>> for more
  20. details) |Required |
  21. |`format` |format to apply to the output value of this aggregation |Optional, defaults to `null` |
  22. |===
  23. The following snippet calculates the cumulative sum of the total monthly `sales`:
  24. [source,js]
  25. --------------------------------------------------
  26. {
  27. "aggs" : {
  28. "sales_per_month" : {
  29. "date_histogram" : {
  30. "field" : "date",
  31. "interval" : "month"
  32. },
  33. "aggs": {
  34. "sales": {
  35. "sum": {
  36. "field": "price"
  37. }
  38. },
  39. "cumulative_sales": {
  40. "cumulative_sum": {
  41. "buckets_paths": "sales" <1>
  42. }
  43. }
  44. }
  45. }
  46. }
  47. }
  48. --------------------------------------------------
  49. <1> `bucket_paths` instructs this cumulative sum aggregation to use the output of the `sales` aggregation for the cumulative sum
  50. And the following may be the response:
  51. [source,js]
  52. --------------------------------------------------
  53. {
  54. "aggregations": {
  55. "sales_per_month": {
  56. "buckets": [
  57. {
  58. "key_as_string": "2015/01/01 00:00:00",
  59. "key": 1420070400000,
  60. "doc_count": 3,
  61. "sales": {
  62. "value": 550
  63. },
  64. "cumulative_sales": {
  65. "value": 550
  66. }
  67. },
  68. {
  69. "key_as_string": "2015/02/01 00:00:00",
  70. "key": 1422748800000,
  71. "doc_count": 2,
  72. "sales": {
  73. "value": 60
  74. },
  75. "cumulative_sales": {
  76. "value": 610
  77. }
  78. },
  79. {
  80. "key_as_string": "2015/03/01 00:00:00",
  81. "key": 1425168000000,
  82. "doc_count": 2,
  83. "sales": {
  84. "value": 375
  85. },
  86. "cumulative_sales": {
  87. "value": 985
  88. }
  89. }
  90. ]
  91. }
  92. }
  93. }
  94. --------------------------------------------------