Multiple filters on one aggregate in elasticsearch

I am using elasticsearch for a python client. I would like to filter the multiples field in the same aggregate bucket. First, enter some data

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"] }'

Then ask him!

{'_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}

When I run this query, I get a parsing error

TransportError(400, 'parsing_exception', 'Expected [START_OBJECT] under [filter], but got a [START_ARRAY] in [facet1_aggregation]')
+4
source share
1 answer

Almost there. I believe that you should put an array of your queries termsin bool.must.

The error you get says that it expects an object under filter, but gets an array instead. Here, filterthere can be any valid query, and the query list is not valid.

Hope this helps!

+1
source

Source: https://habr.com/ru/post/1692214/


All Articles