【问题标题】:Scoping an elasticsearch aggregation with a query and filter使用查询和过滤器限定弹性搜索聚合
【发布时间】:2018-01-23 04:56:32
【问题描述】:

我正在尝试根据查询和过滤器确定聚合范围。我可以做以下事情:

  1. 将聚合范围限定为全局,但我只能指定过滤器以进一步缩小聚合结果集。我无法指定查询(或 multi_match)。
  2. 继承整个 elasticsearch 查询的范围(所以 multi_match),但我无法删除在整个请求中指定的过滤器。

这里是1的例子:

{
  'colors' => {
    global: {},
    aggs: {
      results: {
        filter: {
          bool: {
            must: my_filters_without_this_filter(filter_to_ignore),
          },
        },
        aggs: {
          results: {
            terms: {
              field: 'color',
              size: 10,
            },
          },
        },
      },
    },
  },
}

问题是我无法为上述聚合指定搜索字符串。

我的另一个选择是简单地继承整个查询,但是我无法删除过滤器:

{
    "aggs": {
        "colors": {
            "filter": {
                "bool": {
                    "must": [, {
                        "terms": {
                            "colors": ["red", "white", "blue", "green"]
                        }
                    }]
                }
            },
            "aggs": {
                "results": {
                    "terms": {
                        "field": "colors",
                        "size": 10
                    }
                }
            }
        },
    },
    "query": {
        "bool": {
            "must": {
                "function_score": {
                    "boost_mode": "replace",
                    "functions": [{
                        "script_score": {
                            "script": "my_script"
                        }
                    }],
                    "query": {
                        "multi_match": {
                            "query": "",
                            "type": "cross_fields",
                            "fields": ["name^6", "description", "barcode^2"],
                            "operator": "and",
                            "zero_terms_query": "all"
                        }
                    }
                }
            },
            "filter": {
                "bool": {
                    "must": [{
                        "terms": {
                            "colors": ["red"]
                        }
                    }]
                }
            }
        }
    }
}

在上述查询中,出于聚合目的,我想忽略指定的过滤器(红色)。

是否可以有一个全局范围的聚合,但也可以指定一个过滤器和一个查询?

我不希望每个请求进行 2 个 elasticsearch 查询,但看起来我可能需要这样做。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    通过使用 post_filter 解决,因此聚合在最终过滤器之前构建。

    【讨论】:

      猜你喜欢
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-20
      • 1970-01-01
      • 2014-07-11
      相关资源
      最近更新 更多