0
点赞
收藏
分享

微信扫一扫

Elasticsearch 7.X AggregationBuliders 相关聚合函数(二)桶聚合2.0

七公子706 2022-02-19 阅读 74
  • global 全局聚合

            
    • 定义搜索执行上下文中所有文档的单个存储桶。此上下文由要搜索的索引和文档类型定义,但不受搜索查询‎‎本身的影响。‎
    • ‎全局聚合器只能作为顶级聚合器放置,因为将全局聚合器嵌入到另一个存储桶聚合器中没有意义。‎
    • 请求示例:
    • POST /sales/_search?size=0
      {
        "query": {
          "match": { "type": "t-shirt" }
        },
        "aggs": {
          "all_products": {
            "global": {}, 
            "aggs": {     
            "avg_price": { "avg": { "field": "price" } }
            }
          },
          "t_shirts": { "avg": { "field": "price" } }
        }
      }

      注意到global中是个空体,其下有个subaggreatons子聚合求平均值;‎

    • 上面的聚合演示了如何计算搜索上下文中所有文档的聚合,而不管查询如何(在我们的示例中,它将计算目录中所有产品的平均价格,而不仅仅是上面query查询条件匹配的"t-shirt")

    • 返回示例:

  • {
      ...
      "aggregations": {
        "all_products": {
          "doc_count": 7, 
          "avg_price": {
            "value": 140.71428571428572 
          }
        },
        "t_shirts": {
          "value": 128.33333333333334 
        }
      }
    }

    ‎已聚合的文档数(在本例中为搜索上下文中的所有文档)‎为7个
    ‎所有产品的平均价格‎为140.7128...
    ‎所有t-shirts的平均价格‎为128.33333...

  • missing 缺少聚合

    • 基于字段数据的单个存储桶聚合,用于创建当前文档集中上下文中缺少字段值(实际上缺少字段或设置了配置的 NULL 值)的所有文档的存储桶。此聚合器通常与其他字段数据存储桶聚合器(如区域)结合使用,以返回由于缺少字段数据值而无法放置在任何其他存储桶中的所有文档的信息。
    • 请求示例:
    • POST /sales/_search?size=0
      {
        "aggs": {
          "products_without_a_price": {
            "missing": { "field": "price" }
          }
        }
      }

      查询确实price字段的数据

    • 返回示例:

    • {
        ...
        "aggregations": {
          "products_without_a_price": {
            "doc_count": 0
          }
        }
      }

      查找到0个没有价格的数据

  • nested 嵌套聚合

    • 一种特殊的单个存储桶聚合,支持聚合嵌套文档
    • 通常用于嵌套nested字段的聚合统计,‎例如,假设我们有一个产品索引,每个产品都有经销商列表 - 每个产品都有自己的产品价格。‎对应的映射如:
    • "mappings": {
          "properties": {
            "resellers": { 
              "type": "nested",
              "properties": {
                "reseller": {
                  "type": "keyword"
                },
                "price": {
                  "type": "double"
                }
              }
            }
          }
        }
    • 请求示例:
    • GET /products/_search?size=0
      {
        "query": {
          "match": {
            "name": "led tv"
          }
        },
        "aggs": {
          "resellers": {
            "nested": {
              "path": "resellers"
            },
            "aggs": {
              "min_price": {
                "min": {
                  "field": "resellers.price"
                }
              }
            }
          }
        }
      }
    • 需要指定嵌套的路径path,并且ffield字段前需要指明

    • 同样支持filter aggregations,子聚合中再次过滤具体条件,详细:嵌套聚合

    • 返回示例:

    • {
        ...
        "aggregations": {
          "resellers": {
            "doc_count": 2,
            "min_price": {
              "value": 350.0
            }
          }
        }
      }

      嵌套文档找到2个,其中price最小的价格是350

  • reverseNested 反向嵌套聚合

    • ‎一种特殊的单个存储桶聚合,支持从嵌套文档聚合父文档。实际上,此聚合可以脱离嵌套块结构并链接到其他嵌套结构或根文档,从而允许在嵌套聚合中嵌套不属于嵌套对象的其他聚合。‎

    • 比如以下这种文档结构:

    • "mappings": {
          "properties": {
            "tags": { "type": "keyword" },
            "comments": {                            
              "type": "nested",
              "properties": {
                "username": { "type": "keyword" },
                "comment": { "type": "text" }
              }
            }
          }
        }
    • 请求示例:
    • GET /issues/_search
      {
        "query": {
          "match_all": {}
        },
        "aggs": {
          "comments": {
            "nested": {
              "path": "comments"
            },
            "aggs": {
              "top_usernames": {
                "terms": {
                  "field": "comments.username"
                },
                "aggs": {
                  "comment_to_issue": {
                    "reverse_nested": {}, 
                    "aggs": {
                      "top_tags_per_comment": {
                        "terms": {
                          "field": "tags"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    • 第一层聚合嵌套字段中的username,第二层子聚合跳出嵌套字段的tags字段;
    • 返回示例
    • {
        "aggregations": {
          "comments": {
            "doc_count": 1,
            "top_usernames": {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 0,
              "buckets": [
                {
                  "key": "username_1",
                  "doc_count": 1,
                  "comment_to_issue": {
                    "doc_count": 1,
                    "top_tags_per_comment": {
                      "doc_count_error_upper_bound" : 0,
                      "sum_other_doc_count" : 0,
                      "buckets": [
                        {
                          "key": "tag_1",
                          "doc_count": 1
                        }
                        ...
                      ]
                    }
                  }
                }
                ...
              ]
            }
          }
        }
      }

      返回每个username下面有多少种tag,并且每个tag有多少数据

  • terms

    • 常用聚合具体某个字段,返回多个存储桶,类似SQL中的group by
    • 基于多存储桶值源的聚合,其中存储桶是动态构建的 - 每个唯一值一个。‎
    • 请求示例:
    • GET /_search
      {
        "aggs": {
          "genres": {
            "terms": { "field": "genre" }
          }
        }
      }

      返回:

    • {
        ...
        "aggregations": {
          "genres": {
            "doc_count_error_upper_bound": 0,   
            "sum_other_doc_count": 0,           
            "buckets": [                        
              {
                "key": "electronic",
                "doc_count": 6
              },
              {
                "key": "rock",
                "doc_count": 3
              },
              {
                "key": "jazz",
                "doc_count": 2
              }
            ]
          }
        }
      }

举报

相关推荐

0 条评论