Condition OR in Nested Query

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

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"]
                        }
                    }
                ]
            }
        }
    }
}
1 Like

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