# 平均值桶聚合( Avg Bucket Aggregation)

> 警告:此功能是实验性的，可能会在将来的版本中完全更改或删除。Elastic将采取最大的努力来解决此问题，但实验功能不受SLA官方功能的支持。

## 语法 <a href="#avgbucketaggregation-yu-fa" id="avgbucketaggregation-yu-fa"></a>

avg\_bucket聚合结构如下：

```
{
    "avg_bucket": {
        "buckets_path": "the_sum"
    }
}
```

avg\_bucket参数如下：

| 参数名称          | 描述                                                                                                                                                                                                                                                                                          | 是否必填 | 默认值  |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---- | ---- |
| buckets\_path | 想要计算平均值的桶路径，点击[the section called “`buckets_path` Syntax](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-aggregations-pipeline.html#buckets-path-syntax)[edit](https://github.com/elastic/elasticsearch/edit/5.4/docs/reference/aggregations/pipeline.asciidoc)”查看更多细节  | 必填   |      |
| gap\_policy   | 当数据缺口出现时采用的策略，点击[the section called “Dealing with gaps in the data](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-aggregations-pipeline.html#gap-policy)[edit](https://github.com/elastic/elasticsearch/edit/5.4/docs/reference/aggregations/pipeline.asciidoc)”查看更多细节 | 可选   | skip |
| format        | 用于规范聚合输出值的格式                                                                                                                                                                                                                                                                                | 可选   | null |

以下代码段计算每月销售总额的平均值：

```
POST /_search
{
  "size": 0,
  "aggs": {
    "sales_per_month": {
      "date_histogram": {
        "field": "date",
        "interval": "month"
      },
      "aggs": {
        "sales": {
          "sum": {
            "field": "price"
          }
        }
      }
    },
    "avg_monthly_sales": {
      "avg_bucket": {
        "buckets_path": "sales_per_month>sales" #1
      }
    }
  }
}
```

| 1 | buckets\_path指示这个avg\_bucket聚合是想要得到sales\_per\_month日期直方图聚合中sales聚合值的平均值。 |
| - | ------------------------------------------------------------------------- |

可能得到如下的响应：

```
{
   "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
      }
   }
}
```
