stats-bucket-aggregation.asciidoc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. [[search-aggregations-pipeline-stats-bucket-aggregation]]
  2. === Stats Bucket Aggregation
  3. coming[2.1.0]
  4. experimental[]
  5. A sibling pipeline aggregation which calculates a variety of stats 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 `stats_bucket` aggregation looks like this in isolation:
  9. [source,js]
  10. --------------------------------------------------
  11. {
  12. "stats_bucket": {
  13. "buckets_path": "the_sum"
  14. }
  15. }
  16. --------------------------------------------------
  17. .`stats_bucket` Parameters
  18. |===
  19. |Parameter Name |Description |Required |Default Value
  20. |`buckets_path` |The path to the buckets we wish to calculate stats for (see <<buckets-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 | `skip`
  24. |`format` |format to apply to the output value of this aggregation |Optional | `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. "stats_monthly_sales": {
  45. "stats_bucket": {
  46. "buckets_paths": "sales_per_month>sales" <1>
  47. }
  48. }
  49. }
  50. }
  51. --------------------------------------------------
  52. <1> `bucket_paths` instructs this `stats_bucket` aggregation that we want the calculate stats for 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. "stats_monthly_sales": {
  88. "count": 3,
  89. "min": 60,
  90. "max": 550,
  91. "avg": 328.333333333,
  92. "sum": 985
  93. }
  94. }
  95. }
  96. --------------------------------------------------