cumulative-sum-aggregation.asciidoc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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,js]
  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. // CONSOLE
  55. // TEST[setup:sales]
  56. <1> `buckets_path` instructs this cumulative sum aggregation to use the output of the `sales` aggregation for the cumulative sum
  57. And the following may be the response:
  58. [source,js]
  59. --------------------------------------------------
  60. {
  61. "took": 11,
  62. "timed_out": false,
  63. "_shards": ...,
  64. "hits": ...,
  65. "aggregations": {
  66. "sales_per_month": {
  67. "buckets": [
  68. {
  69. "key_as_string": "2015/01/01 00:00:00",
  70. "key": 1420070400000,
  71. "doc_count": 3,
  72. "sales": {
  73. "value": 550.0
  74. },
  75. "cumulative_sales": {
  76. "value": 550.0
  77. }
  78. },
  79. {
  80. "key_as_string": "2015/02/01 00:00:00",
  81. "key": 1422748800000,
  82. "doc_count": 2,
  83. "sales": {
  84. "value": 60.0
  85. },
  86. "cumulative_sales": {
  87. "value": 610.0
  88. }
  89. },
  90. {
  91. "key_as_string": "2015/03/01 00:00:00",
  92. "key": 1425168000000,
  93. "doc_count": 2,
  94. "sales": {
  95. "value": 375.0
  96. },
  97. "cumulative_sales": {
  98. "value": 985.0
  99. }
  100. }
  101. ]
  102. }
  103. }
  104. }
  105. --------------------------------------------------
  106. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  107. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  108. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]