4

I am using elasticsearch on python client. I would like to filter on multiples field on the same aggregation bucket. First, let's inject 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 let's query it !

{'_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 parse error

TransportError(400, 'parsing_exception', 'Expected [START_OBJECT] under [filter], but got a [START_ARRAY] in [facet1_aggregation]')
g.lahlou
  • 468
  • 3
  • 15
  • Can you specify your intent in more detail? This looks like a parsing problem. The input form question is even not a valid JSON. – ttylec Jan 15 '18 at 15:12
  • @ttylec it's valid JSON à-la Python :-) – Val Jan 15 '18 at 15:13
  • However, @g.lahlou it would help if you properly format your query – Val Jan 15 '18 at 15:13
  • @Val, my emacs says that braces don't match :( so if something is missing, it might be missing from more than one place. That could affect the statement of question. – ttylec Jan 15 '18 at 15:21
  • You're missing two closing curly braces at the end – Val Jan 15 '18 at 15:23
  • As requested here is the whole query with no missing bracket. Sorry about that ! – g.lahlou Jan 15 '18 at 16:14

1 Answers1

1

Almost there. I believe you should put the array of your terms queries in a bool.must.

The error you are getting says it expects an object under filter but gets an array instead. Here in filter any valid query could go, and a list of queries is not a valid query.

Hope that helps!

Nikolay Vasiliev
  • 5,656
  • 22
  • 31