首页 > 解决方案 > 如何在 ElasticSearch 中展平深度聚合?

问题描述

我在索引中的文档如下所示:

{
  "foo": null,
  "bars": [
    {
      "baz": "BAZ",
      "qux": null,
      "bears": [
        {
          "fruit": "banana"
        }
      ]
    }
  ]
}

我想.bars[].bears[].fruit为每个找到的值汇总具有计数的唯一值。但是,我也只想计算与 上的某些条件匹配的文档的这些深度值,以及与 和 上的某些条件匹配foo的值。我还想汇总所有文档,忽略搜索查询的内容。bars[]bazqux

以下查询完成了我想做的所有事情:

{
  "aggs": {
    "global": {
      "global": {},
      "aggs": {
        "notFoo": {
          "filter": {
            "bool": {
              "must_not": [
                {
                  "exists": {
                    "field": "foo"
                  }
                }
              ]
            }
          },
          "aggs": {
            "bars": {
              "nested": {
                "path": "bars"
              },
              "aggs": {
                "notValueN": {
                  "filter": {
                    "bool": {
                      "filter": [
                        {
                          "bool": {
                            "should": [
                              {
                                "terms": {
                                  "bars.baz": [
                                    "value1",
                                    "value2",
                                    "value3"
                                  ]
                                }
                              },
                              {
                                "terms": {
                                  "bars.qux": [
                                    "value4",
                                    "value5",
                                    "value6"
                                  ]
                                }
                              }
                            ],
                            "minimum_should_match": 1
                          }
                        }
                      ]
                    }
                  },
                  "aggs": {
                    "bears": {
                      "nested": {
                        "path": "bars.bears"
                      },
                      "aggs": {
                        "rules": {
                          "terms": {
                            "field": "bars.bears.fruit"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

此查询有效,但感觉相当大且繁重。为了得到我正在寻找的结果,我必须访问.aggregations.global.bars.notValueN.bears.fruit.buckets. 有没有办法展平这个大查询?就目前而言,如果以后需要引入任何其他条件,则此查询很难维护。

标签: elasticsearchelasticsearch-aggregation

解决方案


ES 唯一支持对象键展平的地方是集群设置 API。不幸的是,这种策略不能用于 API 的其他部分,包括聚合。

不过,还有一些其他技巧值得一提。

1. 首先是聚合元数据。

负责对高度嵌套的聚合结果进行后处理的任何人都会喜欢知道目标存储桶路径。您可以通过聚合元数据子句提供它:

POST your-index/_search
{
  "aggs": {
    "global": {
      "global": {},
      "meta": {
        "accessor_path": "aggs.global.Foo...."  <---
      },
      ...

这将返回

{
  "aggregations" : {
    "global" : {
      "meta" : {
        "accessor_path" : "aggs.global.Foo..."  <---
      },
      "Foo" : {

2. 然后是响应过滤

如果您在同一个请求正文中包含多个(子)聚合,则可以通过filter_pathURI 参数减少响应“体积” :

POST your-index/_search/template?filter_path=aggregations.global.meta,aggregations.global.*.*.*.*.*.buckets
{
  "aggs": {
    "global": {
      ...

这可能会或可能不会真正帮助您,因为您的 agg 查询看起来很简单并且没有太多子条款。

3. 最后说一下可维护性

在处理可重用查询时,Elasticsearch 提供了搜索模板 API。您将构建一个包含参数化mustache模板的脚本,然后在查询时提供参数。

在您的特定用例中,我提出以下建议:

  1. 存储 mustache 模板脚本:
POST _scripts/nested_bars_query
{
  "script": {
    "lang": "mustache",
    "source": """
      {
        "query": {{#toJson}}raw_search_query{{/toJson}},
        "aggs": {
          "global": {
            "global": {},
            "meta": {
              "accessor_path": "{{accessor_path}}"
            },
            "aggs": {
              "{{notXYZ.agg_name}}": {
                "filter": {
                  "bool": {
                    "must_not": [
                      {
                        "exists": {
                          "field": "{{notXYZ.field_name}}"
                        }
                      }
                    ]
                  }
                },
                "aggs": {
                  "bars": {
                    "nested": {
                      "path": "bars"
                    },
                    "aggs": {
                      "{{notValueN.agg_name}}": {
                        "filter": {
                          "bool": {
                            "filter": [
                              {
                                "bool": {
                                  "should":  {{#toJson}}notValueN.raw_should_clauses{{/toJson}},
                                  "minimum_should_match": 1
                                }
                              }
                            ]
                          }
                        },
                        "aggs": {
                          "bears": {
                            "nested": {
                              "path": "bars.bears"
                            },
                            "aggs": {
                              "rules": {
                                "terms": {
                                  "field": "bars.bears.fruit"
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    """
  }
}
  1. /_search/template/使用搜索模板 ID ( )定位端点nested_bars_query。此外,指定上面讨论的filter_path和元数据:accessor_path
POST your-index-name/_search/template?filter_path=aggregations.global.meta,aggregations.global.*.*.*.*.*.buckets
{
  "id": "nested_bars_query",
  "params": {
    "raw_search_query": {
      "match_all": {}
    },
    "accessor_path": "aggs.global.Foo.bars.notValueN.bears.rules.buckets",
    "notXYZ": {
      "agg_name": "Foo",
      "field_name": "foo"
    },
    "notValueN": {
      "agg_name": "notValueN",
      "raw_should_clauses": [
        {
          "terms": {
            "bars.baz": [
              "BAZ",
              "value2",
              "value3"
            ]
          }
        },
        {
          "terms": {
            "bars.qux": [
              "value4",
              "value5",
              "value6"
            ]
          }
        }
      ]
    }
  }
}

agg_name您当然可以通过消除定义 custom等的可能性来标准化上述内容。

如果以后需要引入额外的条件,可以修改raw_should_clauses里面的列表params


推荐阅读