Global Aggregation(全局聚合)

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-global-aggregation.html

译文链接 : http://www.apache.wiki/display/Elasticsearch(修改该链接为 ApacheCN 对应的译文链接)

贡献者 : @于永超,ApacheCNApache中文网

Global Aggregation

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

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

例子:

  1. POST /sales/_search?size=0
  2. {
  3. "query" : {
  4. "match" : { "type" : "t-shirt" }
  5. },
  6. "aggs" : {
  7. "all_products" : {
  8. "global" : {}, 1
  9. "aggs" : { 2
  10. "avg_price" : { "avg" : { "field" : "price" } }
  11. }
  12. },
  13. "t_shirts": { "avg" : { "field" : "price" } }
  14. }
  15. }

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

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

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

上述聚合的响应结果:

  1. {
  2. ...
  3. "aggregations" : {
  4. "all_products" : {
  5. "doc_count" : 7, 1
  6. "avg_price" : {
  7. "value" : 140.71428571428572 2
  8. }
  9. },
  10. "t_shirts": {
  11. "value" : 128.33333333333334 3
  12. }
  13. }
  14. }

#1 汇总的文档数量(在我们的例子中,搜索范围内的所有文档)

#2 所有产品的平均价格

#3 所有“t_shirts”的平均价格