2015-05-21 17:39:38 +08:00
|
|
|
[[search-aggregations-pipeline-avg-bucket-aggregation]]
|
2021-03-03 02:44:43 +08:00
|
|
|
=== Average bucket aggregation
|
2020-10-31 01:25:21 +08:00
|
|
|
++++
|
2021-03-03 02:44:43 +08:00
|
|
|
<titleabbrev>Average bucket</titleabbrev>
|
2020-10-31 01:25:21 +08:00
|
|
|
++++
|
2015-05-06 19:54:42 +08:00
|
|
|
|
2021-03-03 02:44:43 +08:00
|
|
|
A sibling pipeline aggregation which calculates the mean value of a specified
|
|
|
|
metric in a sibling aggregation. The specified metric must be numeric and the
|
|
|
|
sibling aggregation must be a multi-bucket aggregation.
|
2015-05-06 19:54:42 +08:00
|
|
|
|
2019-04-30 22:19:09 +08:00
|
|
|
[[avg-bucket-agg-syntax]]
|
2015-05-06 19:54:42 +08:00
|
|
|
==== Syntax
|
|
|
|
|
2021-03-03 02:44:43 +08:00
|
|
|
[source,js,indent=0]
|
|
|
|
----
|
|
|
|
include::avg-bucket-aggregation.asciidoc[tag=avg-bucket-agg-syntax]
|
|
|
|
----
|
2017-05-02 01:30:51 +08:00
|
|
|
// NOTCONSOLE
|
2015-05-06 19:54:42 +08:00
|
|
|
|
2019-04-30 22:19:09 +08:00
|
|
|
[[avg-bucket-params]]
|
2021-03-03 02:44:43 +08:00
|
|
|
==== Parameters
|
|
|
|
|
|
|
|
`buckets_path`::
|
|
|
|
(Required, string)
|
|
|
|
Path to the buckets to average. For syntax, see <<buckets-path-syntax>>.
|
|
|
|
|
|
|
|
`gap_policy`::
|
|
|
|
(Optional, string)
|
|
|
|
Policy to apply when gaps are found in the data. For valid values, see
|
2021-03-03 22:31:02 +08:00
|
|
|
<<gap-policy>>. Defaults to `skip`.
|
2021-03-03 02:44:43 +08:00
|
|
|
|
|
|
|
`format`::
|
|
|
|
(Optional, string)
|
|
|
|
{javadoc}/java.base/java/text/DecimalFormat.html[DecimalFormat pattern] for the
|
|
|
|
output value. If specified, the formatted value is returned in the aggregation's
|
|
|
|
`value_as_string` property.
|
|
|
|
|
|
|
|
[[avg-bucket-agg-response]]
|
|
|
|
==== Response body
|
|
|
|
|
|
|
|
`value`::
|
|
|
|
(float)
|
|
|
|
Mean average value for the metric specified in `buckets_path`.
|
|
|
|
|
|
|
|
`value_as_string`::
|
|
|
|
(string)
|
|
|
|
Formatted output value for the aggregation. This property is only provided if
|
|
|
|
a `format` is specified in the request.
|
|
|
|
|
|
|
|
[[avg-bucket-agg-ex]]
|
|
|
|
==== Example
|
|
|
|
|
|
|
|
The following `avg_monthly_sales` aggregation uses `avg_bucket` to calculate
|
|
|
|
average sales per month:
|
|
|
|
|
|
|
|
[source,console,subs="specialchars+"]
|
|
|
|
----
|
|
|
|
POST _search
|
2015-05-06 19:54:42 +08:00
|
|
|
{
|
2016-08-13 06:42:19 +08:00
|
|
|
"size": 0,
|
|
|
|
"aggs": {
|
|
|
|
"sales_per_month": {
|
|
|
|
"date_histogram": {
|
|
|
|
"field": "date",
|
Force selection of calendar or fixed intervals in date histo agg (#33727)
The date_histogram accepts an interval which can be either a calendar
interval (DST-aware, leap seconds, arbitrary length of months, etc) or
fixed interval (strict multiples of SI units). Unfortunately this is inferred
by first trying to parse as a calendar interval, then falling back to fixed
if that fails.
This leads to confusing arrangement where `1d` == calendar, but
`2d` == fixed. And if you want a day of fixed time, you have to
specify `24h` (e.g. the next smallest unit). This arrangement is very
error-prone for users.
This PR adds `calendar_interval` and `fixed_interval` parameters to any
code that uses intervals (date_histogram, rollup, composite, datafeed, etc).
Calendar only accepts calendar intervals, fixed accepts any combination of
units (meaning `1d` can be used to specify `24h` in fixed time), and both
are mutually exclusive.
The old interval behavior is deprecated and will throw a deprecation warning.
It is also mutually exclusive with the two new parameters. In the future the
old dual-purpose interval will be removed.
The change applies to both REST and java clients.
2019-05-07 05:17:11 +08:00
|
|
|
"calendar_interval": "month"
|
2016-08-13 06:42:19 +08:00
|
|
|
},
|
|
|
|
"aggs": {
|
|
|
|
"sales": {
|
|
|
|
"sum": {
|
|
|
|
"field": "price"
|
|
|
|
}
|
2015-05-06 19:54:42 +08:00
|
|
|
}
|
2016-08-13 06:42:19 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"avg_monthly_sales": {
|
2021-03-03 02:44:43 +08:00
|
|
|
// tag::avg-bucket-agg-syntax[] <1>
|
2016-08-13 06:42:19 +08:00
|
|
|
"avg_bucket": {
|
2021-03-03 02:44:43 +08:00
|
|
|
"buckets_path": "sales_per_month>sales",
|
|
|
|
"gap_policy": "skip",
|
|
|
|
"format": "#,##0.00;(#,##0.00)"
|
2016-08-13 06:42:19 +08:00
|
|
|
}
|
2021-03-03 02:44:43 +08:00
|
|
|
// end::avg-bucket-agg-syntax[] <2>
|
2015-05-06 19:54:42 +08:00
|
|
|
}
|
2016-08-13 06:42:19 +08:00
|
|
|
}
|
2015-05-06 19:54:42 +08:00
|
|
|
}
|
2021-03-03 02:44:43 +08:00
|
|
|
----
|
2016-08-13 06:42:19 +08:00
|
|
|
// TEST[setup:sales]
|
2019-09-05 00:51:02 +08:00
|
|
|
|
2021-03-03 02:44:43 +08:00
|
|
|
<1> Start of the `avg_bucket` configuration. Comment is not part of the example.
|
|
|
|
<2> End of the `avg_bucket` configuration. Comment is not part of the example.
|
2015-05-06 19:54:42 +08:00
|
|
|
|
2021-03-03 02:44:43 +08:00
|
|
|
The request returns the following response:
|
2015-05-06 19:54:42 +08:00
|
|
|
|
2019-09-07 02:05:36 +08:00
|
|
|
[source,console-result]
|
2021-03-03 02:44:43 +08:00
|
|
|
----
|
2015-05-06 19:54:42 +08:00
|
|
|
{
|
2021-03-03 02:44:43 +08:00
|
|
|
"took": 11,
|
|
|
|
"timed_out": false,
|
|
|
|
"_shards": ...,
|
|
|
|
"hits": ...,
|
|
|
|
"aggregations": {
|
|
|
|
"sales_per_month": {
|
|
|
|
"buckets": [
|
|
|
|
{
|
|
|
|
"key_as_string": "2015/01/01 00:00:00",
|
|
|
|
"key": 1420070400000,
|
|
|
|
"doc_count": 3,
|
|
|
|
"sales": {
|
|
|
|
"value": 550.0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"key_as_string": "2015/02/01 00:00:00",
|
|
|
|
"key": 1422748800000,
|
|
|
|
"doc_count": 2,
|
|
|
|
"sales": {
|
|
|
|
"value": 60.0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"key_as_string": "2015/03/01 00:00:00",
|
|
|
|
"key": 1425168000000,
|
|
|
|
"doc_count": 2,
|
|
|
|
"sales": {
|
|
|
|
"value": 375.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"avg_monthly_sales": {
|
|
|
|
"value": 328.33333333333333,
|
|
|
|
"value_as_string": "328.33"
|
|
|
|
}
|
|
|
|
}
|
2015-05-06 19:54:42 +08:00
|
|
|
}
|
2021-03-03 02:44:43 +08:00
|
|
|
----
|
2016-08-13 06:42:19 +08:00
|
|
|
// TESTRESPONSE[s/"took": 11/"took": $body.took/]
|
|
|
|
// TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
|
|
|
|
// TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
|