sum-bucket-aggregation.asciidoc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. [[search-aggregations-pipeline-sum-bucket-aggregation]]
  2. === Sum Bucket Aggregation
  3. A sibling pipeline aggregation which calculates the sum across all bucket of a specified metric in a sibling aggregation.
  4. The specified metric must be numeric and the sibling aggregation must be a multi-bucket aggregation.
  5. ==== Syntax
  6. A `sum_bucket` aggregation looks like this in isolation:
  7. [source,js]
  8. --------------------------------------------------
  9. {
  10. "sum_bucket": {
  11. "buckets_path": "the_sum"
  12. }
  13. }
  14. --------------------------------------------------
  15. .`sum_bucket` Parameters
  16. |===
  17. |Parameter Name |Description |Required |Default Value
  18. |`buckets_path` |The path to the buckets we wish to find the sum for (see <<bucket-path-syntax>> for more
  19. details) |Required |
  20. |`gap_policy` |The policy to apply when gaps are found in the data (see <<gap-policy>> for more
  21. details)|Optional, defaults to `skip` ||
  22. |`format` |format to apply to the output value of this aggregation |Optional, defaults to `null` |
  23. |===
  24. The following snippet calculates the sum of all the total monthly `sales` buckets:
  25. [source,js]
  26. --------------------------------------------------
  27. {
  28. "aggs" : {
  29. "sales_per_month" : {
  30. "date_histogram" : {
  31. "field" : "date",
  32. "interval" : "month"
  33. },
  34. "aggs": {
  35. "sales": {
  36. "sum": {
  37. "field": "price"
  38. }
  39. }
  40. }
  41. },
  42. "sum_monthly_sales": {
  43. "sum_bucket": {
  44. "buckets_paths": "sales_per_month>sales" <1>
  45. }
  46. }
  47. }
  48. }
  49. --------------------------------------------------
  50. <1> `bucket_paths` instructs this sum_bucket aggregation that we want the sum of the `sales` aggregation in the
  51. `sales_per_month` date histogram.
  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. },
  67. {
  68. "key_as_string": "2015/02/01 00:00:00",
  69. "key": 1422748800000,
  70. "doc_count": 2,
  71. "sales": {
  72. "value": 60
  73. }
  74. },
  75. {
  76. "key_as_string": "2015/03/01 00:00:00",
  77. "key": 1425168000000,
  78. "doc_count": 2,
  79. "sales": {
  80. "value": 375
  81. }
  82. }
  83. ]
  84. },
  85. "sum_monthly_sales": {
  86. "value": 985
  87. }
  88. }
  89. }
  90. --------------------------------------------------