stats-bucket-aggregation.asciidoc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. [[search-aggregations-pipeline-stats-bucket-aggregation]]
  2. === Stats bucket aggregation
  3. ++++
  4. <titleabbrev>Stats bucket</titleabbrev>
  5. ++++
  6. A sibling pipeline aggregation which calculates a variety of stats across all bucket 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 `stats_bucket` aggregation looks like this in isolation:
  10. [source,js]
  11. --------------------------------------------------
  12. {
  13. "stats_bucket": {
  14. "buckets_path": "the_sum"
  15. }
  16. }
  17. --------------------------------------------------
  18. // NOTCONSOLE
  19. [[stats-bucket-params]]
  20. .`stats_bucket` Parameters
  21. [options="header"]
  22. |===
  23. |Parameter Name |Description |Required |Default Value
  24. |`buckets_path` |The path to the buckets we wish to calculate stats 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` |format to apply to the output value of this aggregation |Optional | `null`
  29. |===
  30. The following snippet calculates the stats for monthly `sales`:
  31. [source,console]
  32. --------------------------------------------------
  33. POST /sales/_search
  34. {
  35. "size": 0,
  36. "aggs": {
  37. "sales_per_month": {
  38. "date_histogram": {
  39. "field": "date",
  40. "calendar_interval": "month"
  41. },
  42. "aggs": {
  43. "sales": {
  44. "sum": {
  45. "field": "price"
  46. }
  47. }
  48. }
  49. },
  50. "stats_monthly_sales": {
  51. "stats_bucket": {
  52. "buckets_path": "sales_per_month>sales" <1>
  53. }
  54. }
  55. }
  56. }
  57. --------------------------------------------------
  58. // TEST[setup:sales]
  59. <1> `bucket_paths` instructs this `stats_bucket` aggregation that we want the calculate stats for the `sales` aggregation in the
  60. `sales_per_month` date histogram.
  61. And the following may be the response:
  62. [source,console-result]
  63. --------------------------------------------------
  64. {
  65. "took": 11,
  66. "timed_out": false,
  67. "_shards": ...,
  68. "hits": ...,
  69. "aggregations": {
  70. "sales_per_month": {
  71. "buckets": [
  72. {
  73. "key_as_string": "2015/01/01 00:00:00",
  74. "key": 1420070400000,
  75. "doc_count": 3,
  76. "sales": {
  77. "value": 550.0
  78. }
  79. },
  80. {
  81. "key_as_string": "2015/02/01 00:00:00",
  82. "key": 1422748800000,
  83. "doc_count": 2,
  84. "sales": {
  85. "value": 60.0
  86. }
  87. },
  88. {
  89. "key_as_string": "2015/03/01 00:00:00",
  90. "key": 1425168000000,
  91. "doc_count": 2,
  92. "sales": {
  93. "value": 375.0
  94. }
  95. }
  96. ]
  97. },
  98. "stats_monthly_sales": {
  99. "count": 3,
  100. "min": 60.0,
  101. "max": 550.0,
  102. "avg": 328.3333333333333,
  103. "sum": 985.0
  104. }
  105. }
  106. }
  107. --------------------------------------------------
  108. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  109. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  110. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]