cumulative-sum-aggregation.asciidoc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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-params]]
  18. .`cumulative_sum` Parameters
  19. [options="header"]
  20. |===
  21. |Parameter Name |Description |Required |Default Value
  22. |`buckets_path` |The path to the buckets we wish to find the cumulative sum for (see <<buckets-path-syntax>> for more
  23. details) |Required |
  24. |`format` |format to apply to the output value of this aggregation |Optional |`null`
  25. |===
  26. The following snippet calculates the cumulative sum of the total monthly `sales`:
  27. [source,console]
  28. --------------------------------------------------
  29. POST /sales/_search
  30. {
  31. "size": 0,
  32. "aggs" : {
  33. "sales_per_month" : {
  34. "date_histogram" : {
  35. "field" : "date",
  36. "calendar_interval" : "month"
  37. },
  38. "aggs": {
  39. "sales": {
  40. "sum": {
  41. "field": "price"
  42. }
  43. },
  44. "cumulative_sales": {
  45. "cumulative_sum": {
  46. "buckets_path": "sales" <1>
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. --------------------------------------------------
  54. // TEST[setup:sales]
  55. <1> `buckets_path` instructs this cumulative sum aggregation to use the output of the `sales` aggregation for the cumulative sum
  56. And the following may be the response:
  57. [source,js]
  58. --------------------------------------------------
  59. {
  60. "took": 11,
  61. "timed_out": false,
  62. "_shards": ...,
  63. "hits": ...,
  64. "aggregations": {
  65. "sales_per_month": {
  66. "buckets": [
  67. {
  68. "key_as_string": "2015/01/01 00:00:00",
  69. "key": 1420070400000,
  70. "doc_count": 3,
  71. "sales": {
  72. "value": 550.0
  73. },
  74. "cumulative_sales": {
  75. "value": 550.0
  76. }
  77. },
  78. {
  79. "key_as_string": "2015/02/01 00:00:00",
  80. "key": 1422748800000,
  81. "doc_count": 2,
  82. "sales": {
  83. "value": 60.0
  84. },
  85. "cumulative_sales": {
  86. "value": 610.0
  87. }
  88. },
  89. {
  90. "key_as_string": "2015/03/01 00:00:00",
  91. "key": 1425168000000,
  92. "doc_count": 2,
  93. "sales": {
  94. "value": 375.0
  95. },
  96. "cumulative_sales": {
  97. "value": 985.0
  98. }
  99. }
  100. ]
  101. }
  102. }
  103. }
  104. --------------------------------------------------
  105. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  106. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  107. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]