1
0

avg-bucket-aggregation.asciidoc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. [[search-aggregations-pipeline-avg-bucket-aggregation]]
  2. === Average bucket aggregation
  3. ++++
  4. <titleabbrev>Average bucket</titleabbrev>
  5. ++++
  6. A sibling pipeline aggregation which calculates the mean value of a specified
  7. metric in a sibling aggregation. The specified metric must be numeric and the
  8. sibling aggregation must be a multi-bucket aggregation.
  9. [[avg-bucket-agg-syntax]]
  10. ==== Syntax
  11. [source,js,indent=0]
  12. ----
  13. include::avg-bucket-aggregation.asciidoc[tag=avg-bucket-agg-syntax]
  14. ----
  15. // NOTCONSOLE
  16. [[avg-bucket-params]]
  17. ==== Parameters
  18. `buckets_path`::
  19. (Required, string)
  20. Path to the buckets to average. For syntax, see <<buckets-path-syntax>>.
  21. `gap_policy`::
  22. (Optional, string)
  23. Policy to apply when gaps are found in the data. For valid values, see
  24. <<gap-policy>>. Defaults to `skip`.
  25. `format`::
  26. (Optional, string)
  27. {javadoc}/java.base/java/text/DecimalFormat.html[DecimalFormat pattern] for the
  28. output value. If specified, the formatted value is returned in the aggregation's
  29. `value_as_string` property.
  30. [[avg-bucket-agg-response]]
  31. ==== Response body
  32. `value`::
  33. (float)
  34. Mean average value for the metric specified in `buckets_path`.
  35. `value_as_string`::
  36. (string)
  37. Formatted output value for the aggregation. This property is only provided if
  38. a `format` is specified in the request.
  39. [[avg-bucket-agg-ex]]
  40. ==== Example
  41. The following `avg_monthly_sales` aggregation uses `avg_bucket` to calculate
  42. average sales per month:
  43. [source,console,subs="specialchars+"]
  44. ----
  45. POST _search
  46. {
  47. "size": 0,
  48. "aggs": {
  49. "sales_per_month": {
  50. "date_histogram": {
  51. "field": "date",
  52. "calendar_interval": "month"
  53. },
  54. "aggs": {
  55. "sales": {
  56. "sum": {
  57. "field": "price"
  58. }
  59. }
  60. }
  61. },
  62. "avg_monthly_sales": {
  63. // tag::avg-bucket-agg-syntax[] <1>
  64. "avg_bucket": {
  65. "buckets_path": "sales_per_month>sales",
  66. "gap_policy": "skip",
  67. "format": "#,##0.00;(#,##0.00)"
  68. }
  69. // end::avg-bucket-agg-syntax[] <2>
  70. }
  71. }
  72. }
  73. ----
  74. // TEST[setup:sales]
  75. <1> Start of the `avg_bucket` configuration. Comment is not part of the example.
  76. <2> End of the `avg_bucket` configuration. Comment is not part of the example.
  77. The request returns the following response:
  78. [source,console-result]
  79. ----
  80. {
  81. "took": 11,
  82. "timed_out": false,
  83. "_shards": ...,
  84. "hits": ...,
  85. "aggregations": {
  86. "sales_per_month": {
  87. "buckets": [
  88. {
  89. "key_as_string": "2015/01/01 00:00:00",
  90. "key": 1420070400000,
  91. "doc_count": 3,
  92. "sales": {
  93. "value": 550.0
  94. }
  95. },
  96. {
  97. "key_as_string": "2015/02/01 00:00:00",
  98. "key": 1422748800000,
  99. "doc_count": 2,
  100. "sales": {
  101. "value": 60.0
  102. }
  103. },
  104. {
  105. "key_as_string": "2015/03/01 00:00:00",
  106. "key": 1425168000000,
  107. "doc_count": 2,
  108. "sales": {
  109. "value": 375.0
  110. }
  111. }
  112. ]
  113. },
  114. "avg_monthly_sales": {
  115. "value": 328.33333333333333,
  116. "value_as_string": "328.33"
  117. }
  118. }
  119. }
  120. ----
  121. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  122. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  123. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]