Using "terms" on a nested field


(Marcelo Theodoro) #1

Hello. I'm using a "term" filter on a nested field, it's working fine. But I'm getting an error when I try to do the same query with "terms" on ES 2.4. Any tips here?

# Working:
{'query': {'filtered': {'filter': {'bool': {'must': [{'term': {'clinic': '7'}}, {'term': {'_deleted': False}}, {'nested': {'query': {'term': {'procedure.name': {'term': 'terapy'}}}, 'path': 'procedure'}}]}}, 'query': {'match_all': {}}}}}
 
# Not working:
{'query': {'filtered': {'filter': {'bool': {'must': [{'term': {'clinic': '7'}}, {'term': {'_deleted': False}}, {'nested': {'query': {'terms': {'category.pk': {'terms': ['522', '511']}}}, 'path': 'category'}}]}}, 'query': {'match_all': {}}}}}
 
 
# Error:
# RequestError: TransportError(400, u'search_phase_execution_exception', u'[terms] query does not support [terms] within lookup element')

Algo, I'm using the lib elasticsearch-dsl to interact with ES on Python. The code is something like:

# Working
search.filter('nested', path='procedure', query=Q('term', procedure__name={'term': procedure_name}))

# Not working
search.filter('nested', path='category', query=Q('terms', category__pk={'terms': category_id}))

(Abdon Pijpelink) #2

The syntax of the terms query is slightly different. Instead of

{"terms": {"category.pk": {"terms": ["522", "511"]}}}

You'll have to use:

{"terms": {"category.pk": ["522", "511"]}}


(Marcelo Theodoro) #3

Thank you abdon. I made this change and now is working correctly. :slight_smile:


(system) #4

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.