avg-bucket-aggregation.asciidoc 3.0 KB

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