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]')
6
  • Can you specify your intent in more detail? This looks like a parsing problem. The input form question is even not a valid JSON. Commented Jan 15, 2018 at 15:12
  • @ttylec it's valid JSON à-la Python :-) Commented Jan 15, 2018 at 15:13
  • However, @g.lahlou it would help if you properly format your query Commented Jan 15, 2018 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. Commented Jan 15, 2018 at 15:21
  • You're missing two closing curly braces at the end Commented Jan 15, 2018 at 15:23

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.