sum-bucket-aggregation.asciidoc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. [[search-aggregations-pipeline-sum-bucket-aggregation]]
  2. === Sum bucket aggregation
  3. ++++
  4. <titleabbrev>Sum bucket</titleabbrev>
  5. ++++
  6. A sibling pipeline aggregation which calculates the sum across all buckets of a specified metric in a sibling aggregation.
  7. The specified metric must be numeric and the sibling aggregation must be a multi-bucket aggregation.
  8. ==== Syntax
  9. A `sum_bucket` aggregation looks like this in isolation:
  10. [source,js]
  11. --------------------------------------------------
  12. {
  13. "sum_bucket": {
  14. "buckets_path": "the_sum"
  15. }
  16. }
  17. --------------------------------------------------
  18. // NOTCONSOLE
  19. [[sum-bucket-params]]
  20. .`sum_bucket` Parameters
  21. [options="header"]
  22. |===
  23. |Parameter Name |Description |Required |Default Value
  24. |`buckets_path` |The path to the buckets we wish to find the sum for (see <<buckets-path-syntax>> for more
  25. details) |Required |
  26. |`gap_policy` |The policy to apply when gaps are found in the data (see <<gap-policy>> for more
  27. details)|Optional | `skip`
  28. |`format` |{javadoc}/java.base/java/text/DecimalFormat.html[DecimalFormat pattern] for the
  29. output value. If specified, the formatted value is returned in the aggregation's
  30. `value_as_string` property. |Optional |`null`
  31. |===
  32. The following snippet calculates the sum of all the total monthly `sales` buckets:
  33. [source,console]
  34. --------------------------------------------------
  35. POST /sales/_search
  36. {
  37. "size": 0,
  38. "aggs": {
  39. "sales_per_month": {
  40. "date_histogram": {
  41. "field": "date",
  42. "calendar_interval": "month"
  43. },
  44. "aggs": {
  45. "sales": {
  46. "sum": {
  47. "field": "price"
  48. }
  49. }
  50. }
  51. },
  52. "sum_monthly_sales": {
  53. "sum_bucket": {
  54. "buckets_path": "sales_per_month>sales" <1>
  55. }
  56. }
  57. }
  58. }
  59. --------------------------------------------------
  60. // TEST[setup:sales]
  61. <1> `buckets_path` instructs this sum_bucket aggregation that we want the sum of the `sales` aggregation in the
  62. `sales_per_month` date histogram.
  63. And the following may be the response:
  64. [source,console-result]
  65. --------------------------------------------------
  66. {
  67. "took": 11,
  68. "timed_out": false,
  69. "_shards": ...,
  70. "hits": ...,
  71. "aggregations": {
  72. "sales_per_month": {
  73. "buckets": [
  74. {
  75. "key_as_string": "2015/01/01 00:00:00",
  76. "key": 1420070400000,
  77. "doc_count": 3,
  78. "sales": {
  79. "value": 550.0
  80. }
  81. },
  82. {
  83. "key_as_string": "2015/02/01 00:00:00",
  84. "key": 1422748800000,
  85. "doc_count": 2,
  86. "sales": {
  87. "value": 60.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. }
  98. ]
  99. },
  100. "sum_monthly_sales": {
  101. "value": 985.0
  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/]