# 全局聚合(Global Aggregation)

定义搜索执行上下文中的所有文档的单个bucket，这个上下文由索引和您正在搜索的文档类型定义，但不受搜索查询本身的影响。

> 全局聚合器只能作为顶层聚合器放置，因为将全局聚合器嵌入到另一个分组聚合器中是没有意义的。

例子：

```
POST /sales/_search?size=0
{
    "query" : {
        "match" : { "type" : "t-shirt" }
    },
    "aggs" : {
        "all_products" : {
            "global" : {}, ＃1
            "aggs" : {  ＃2
                "avg_price" : { "avg" : { "field" : "price" } }
            }
        },
        "t_shirts": { "avg" : { "field" : "price" } }
    }
}
```

\#1 global aggregation(全局聚合)有一个空的体

\#2 为这个global aggregation(全局聚合)注册的子聚合

上面的聚合演示了如何在搜索上下文中的所有文档上计算聚合(本例中的avg\_price),无论查询如何（在我们的示例中，它将计算我们目录中所有产品的平均价格，而不仅仅是在“shirts”）

上述聚合的响应结果：

```
{
    ...
    "aggregations" : {
        "all_products" : {
            "doc_count" : 7, ＃1
            "avg_price" : {
                "value" : 140.71428571428572  ＃2
            }
        },
        "t_shirts": {
            "value" : 128.33333333333334  ＃3
        }
    }
}
```

＃1 汇总的文档数量（在我们的例子中，搜索范围内的所有文档）

＃2 所有产品的平均价格

＃3 所有“t\_shirts”的平均价格


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/36aggregationsju-he-fen-679029/362tong-ju540828-bucketaggregations/quan-ju-ju-540828-global-aggregation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
