sum-bucket-aggregation.asciidoc 2.8 KB

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