# Condition OR in Nested Query

**URL:** <https://discuss.elastic.co/t/condition-or-in-nested-query/125236>\
**Category:** Elasticsearch\
**Created:** [March 22, 2018, 4:59pm UTC](https://discuss.elastic.co/t/condition-or-in-nested-query/125236 "2018-03-22T16:59:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TomTom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tomtom/32/3676_2.png) [@TomTom](https://discuss.elastic.co/u/TomTom)\
**Post date:** [March 22, 2018, 4:59pm UTC](https://discuss.elastic.co/t/condition-or-in-nested-query/125236/1 "2018-03-22T16:59:52Z")

</div>

I have this nested field mapping in Elasticsearch (6.2.2):

```
"customfield_open": {
    "properties": {
        "content": {
            "type": "nested",
            "properties": {
                "id": {
                    "type": "long"
                },
                "value": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        }
    }
}

```

The property ID store a Custom Field ID, and value store the value set by the custom field, example:

> Custom Field: State | ID: 123  
> ID: 123 / Value: NY  
> ID: 123 / Value: TX  
> Custom Field: Organization Nº | ID: 124  
> ID: 124 / Value: Organization ABC  
> ID: 124 / Value: Organization XYZ

One document can have two custom fields (nested mapping):

> ID: 123 / Value: NY  
> ID: 124 / Value: Organization ABC

```
"customer_customfields": [
    {
        "content": [
            {
                "id": 123,
                "value": "NY"
            },
            {
                "id": 124,
                "value": "Organization ABC"
            }
        ]
    }
]

```

How is possible search for documents that have the custom field 123 with the value NY OR TX?  
Example:

The code below does not work:

```
{
    "nested":{
        "path":"customfield_open.content",
        "query":{
            "bool":{
                "must":[
                    {
                        "term":{
                            "customfield_open.content.id":123
                        }
                    },
                    {
                        "term":{
                            "customfield_open.content.value.keyword":["NY", "TX"]
                        }
                    }
                ]
            }
        }
    }
}

```

**Error Reported:**

```
{
	"error": {
		"root_cause": [
			{
				"type": "parsing_exception",
				"reason": "[term] query does not support array of values",
				"line": 28,
				"col": 86
			}
		],
		"type": "parsing_exception",
		"reason": "[term] query does not support array of values",
		"line": 28,
		"col": 86
	},
	"status": 400
}

```

But this code work, but for just a one value:

```
{
    "nested":{
        "path":"customfield_open.content",
        "query":{
            "bool":{
                "must":[
                    {
                        "term":{
                            "customfield_open.content.id":123
                        }
                    },
                    {
                        "term":{
                            "customfield_open.content.value.keyword":"NY"
                        }
                    }
                ]
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![TomTom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tomtom/32/3676_2.png) [@TomTom](https://discuss.elastic.co/u/TomTom)\
**Post date:** [March 22, 2018, 7:20pm UTC](https://discuss.elastic.co/t/condition-or-in-nested-query/125236/2 "2018-03-22T19:20:36Z")

</div>

Problem solved, just change **term** to **terms**.

```
{
    "nested":{
        "path":"customfield_open.content",
        "query":{
            "bool":{
                "must":[
                    {
                        "term":{
                            "customfield_open.content.id":123
                        }
                    },
                    {
                        "terms":{
                            "customfield_open.content.value.keyword":["NY", "TX"]
                        }
                    }
                ]
            }
        }
    }
}
```

---

<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:** [April 19, 2018, 7:20pm UTC](https://discuss.elastic.co/t/condition-or-in-nested-query/125236/3 "2018-04-19T19:20:47Z")

</div>

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