cumulative-sum-aggregation.asciidoc 3.0 KB

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