Elasticsearch:如何同时进行过滤搜索和聚合

Elasticsearch: how to do filtered search and aggregation at the same time

从概念上讲,我需要按照以下方式进行过滤搜索和聚合。

{
    "filtered" : {
        "query": { 
                "match_all" : {
                }
        },
        "aggregations": {
            "facets": {
                "terms": {
                    "field": "subject"
                }
            }
        },
        "filter" : {
            ...
        }
    }
}

以上查询无效,因为我收到以下错误消息:

[filtered] query does not support [aggregations]]

我正在尝试解决这个问题。我在网上找到了 Filter Aggregation 或 Filters Aggregation,但它们似乎不能满足我的需要。

有人可以告诉我可以实现我的目标的正确查询的结构吗?

感谢和问候。

聚合的范围是查询和其中的所有过滤器。这意味着如果您以正常方式提供聚合和查询,它应该可以工作。

{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {}
    }
  },
  "aggregations": {
    "facets": {
      "terms": {
        "field": "subject"
      }
    }
  }
}