avg-bucket-aggregation.asciidoc 3.1 KB

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