Cumulative sum aggregation

Cumulative sum aggregation

A parent pipeline aggregation which calculates the cumulative sum of a specified metric in a parent histogram (or date_histogram) aggregation. The specified metric must be numeric and the enclosing histogram must have min_doc_count set to 0 (default for histogram aggregations).

Syntax

A cumulative_sum aggregation looks like this in isolation:

  1. {
  2. "cumulative_sum": {
  3. "buckets_path": "the_sum"
  4. }
  5. }

Table 60. cumulative_sum Parameters

Parameter NameDescriptionRequiredDefault Value

buckets_path

The path to the buckets we wish to find the cumulative sum for (see buckets_path Syntax for more details)

Required

format

DecimalFormat pattern for the output value. If specified, the formatted value is returned in the aggregation’s value_as_string property

Optional

null

The following snippet calculates the cumulative sum of the total monthly sales:

  1. resp = client.search(
  2. index="sales",
  3. size=0,
  4. aggs={
  5. "sales_per_month": {
  6. "date_histogram": {
  7. "field": "date",
  8. "calendar_interval": "month"
  9. },
  10. "aggs": {
  11. "sales": {
  12. "sum": {
  13. "field": "price"
  14. }
  15. },
  16. "cumulative_sales": {
  17. "cumulative_sum": {
  18. "buckets_path": "sales"
  19. }
  20. }
  21. }
  22. }
  23. },
  24. )
  25. print(resp)
  1. response = client.search(
  2. index: 'sales',
  3. body: {
  4. size: 0,
  5. aggregations: {
  6. sales_per_month: {
  7. date_histogram: {
  8. field: 'date',
  9. calendar_interval: 'month'
  10. },
  11. aggregations: {
  12. sales: {
  13. sum: {
  14. field: 'price'
  15. }
  16. },
  17. cumulative_sales: {
  18. cumulative_sum: {
  19. buckets_path: 'sales'
  20. }
  21. }
  22. }
  23. }
  24. }
  25. }
  26. )
  27. puts response
  1. const response = await client.search({
  2. index: "sales",
  3. size: 0,
  4. aggs: {
  5. sales_per_month: {
  6. date_histogram: {
  7. field: "date",
  8. calendar_interval: "month",
  9. },
  10. aggs: {
  11. sales: {
  12. sum: {
  13. field: "price",
  14. },
  15. },
  16. cumulative_sales: {
  17. cumulative_sum: {
  18. buckets_path: "sales",
  19. },
  20. },
  21. },
  22. },
  23. },
  24. });
  25. console.log(response);
  1. POST /sales/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "sales_per_month": {
  6. "date_histogram": {
  7. "field": "date",
  8. "calendar_interval": "month"
  9. },
  10. "aggs": {
  11. "sales": {
  12. "sum": {
  13. "field": "price"
  14. }
  15. },
  16. "cumulative_sales": {
  17. "cumulative_sum": {
  18. "buckets_path": "sales"
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }

buckets_path instructs this cumulative sum aggregation to use the output of the sales aggregation for the cumulative sum

And the following may be the response:

  1. {
  2. "took": 11,
  3. "timed_out": false,
  4. "_shards": ...,
  5. "hits": ...,
  6. "aggregations": {
  7. "sales_per_month": {
  8. "buckets": [
  9. {
  10. "key_as_string": "2015/01/01 00:00:00",
  11. "key": 1420070400000,
  12. "doc_count": 3,
  13. "sales": {
  14. "value": 550.0
  15. },
  16. "cumulative_sales": {
  17. "value": 550.0
  18. }
  19. },
  20. {
  21. "key_as_string": "2015/02/01 00:00:00",
  22. "key": 1422748800000,
  23. "doc_count": 2,
  24. "sales": {
  25. "value": 60.0
  26. },
  27. "cumulative_sales": {
  28. "value": 610.0
  29. }
  30. },
  31. {
  32. "key_as_string": "2015/03/01 00:00:00",
  33. "key": 1425168000000,
  34. "doc_count": 2,
  35. "sales": {
  36. "value": 375.0
  37. },
  38. "cumulative_sales": {
  39. "value": 985.0
  40. }
  41. }
  42. ]
  43. }
  44. }
  45. }