弹性搜索中单个聚合的多个过滤器

multiple filters on a single aggregation in elasticsearch

我在 python 客户端上使用 elasticsearch。我想在同一个聚合桶上过滤多个字段。 首先,让我们注入一些数据

curl -XPUT 'localhost:9200/data/document/1' -d '{ "facet1": ["a1","b1"], "facet2":["b2"],"facet3":["a3", "c3"] }'

curl -XPUT 'localhost:9200/data/document/2' -d '{ "facet1": ["a1","c1"], "facet2":["b2", "c2"],"facet3":["a3", "c3"] }'

curl -XPUT 'localhost:9200/data/document/3' -d '{ "facet1": ["a1"], "facet2":["b2"],"facet3":["c3"] }'

那我们来查询一下吧!

{'_source': ['facet1',
             'facet2',
             'facet3'],
 'aggregations':
 {'all_products': {'aggregations':
  {'facet1_aggregation': {'aggregations': 
                          {'filtered_facet1': {'terms': {'field': 'facet1'}}},
                           'filter': [{'terms': {'facet2': ['a2 ' 'b2']}},
                                      {'terms': {'facet3': ['a3', 'b3' 'c3']}}]}},
   'facet2_aggregation': {'aggregations': 
                          {'filtered_facet2': {'terms': {'field': 'facet2'}}},
                           'filter': [{'terms': {'facet1': ['a1' 'b1']}},
                                     {'terms': {'facet3': ['a3', 'b3' 'c3']}}]},
  'global': {}}},
 'query': {'bool': 
   {'filter': [{'terms': {'facet1': ['a1']}},
               {'terms': {'facet2': ['a2', 'b2']}}]},
    'must': {'match_all': {}}},
 'size': 10000}

当我 运行 这个查询时,我得到一个解析错误

TransportError(400, 'parsing_exception', 'Expected [START_OBJECT] under [filter], but got a [START_ARRAY] in [facet1_aggregation]')

快到了。我相信您应该将 terms 查询的数组放在 bool.must.

您收到的错误表明它需要 filter 下的一个对象,但却得到了一个数组。在 filter 中,任何有效查询都可以进行,查询列表不是有效查询。

希望对您有所帮助!