# Using "terms" on a nested field

**URL:** <https://discuss.elastic.co/t/using-terms-on-a-nested-field/103383>\
**Category:** Elasticsearch\
**Created:** [October 10, 2017, 1:59pm UTC](https://discuss.elastic.co/t/using-terms-on-a-nested-field/103383 "2017-10-10T13:59:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcelo-Theodoro](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@Marcelo-Theodoro](https://discuss.elastic.co/u/Marcelo-Theodoro)\
**Post date:** [October 10, 2017, 1:59pm UTC](https://discuss.elastic.co/t/using-terms-on-a-nested-field/103383/1 "2017-10-10T13:59:20Z")

</div>

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?

```auto
# 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:

```auto
# 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}))

```

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [October 13, 2017, 12:06pm UTC](https://discuss.elastic.co/t/using-terms-on-a-nested-field/103383/2 "2017-10-13T12:06:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Marcelo-Theodoro](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@Marcelo-Theodoro](https://discuss.elastic.co/u/Marcelo-Theodoro)\
**Post date:** [October 13, 2017, 2:23pm UTC](https://discuss.elastic.co/t/using-terms-on-a-nested-field/103383/3 "2017-10-13T14:23:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 10, 2017, 2:24pm UTC](https://discuss.elastic.co/t/using-terms-on-a-nested-field/103383/4 "2017-11-10T14:24:05Z")

</div>

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