I need to match a complete array of terms with elasticsearch. Only documents that have a array with the same elements should be returned. There should be neither more elements nor a subset of elements in the document's array. The order of elements does not matter.
Example:
filter:
id: ["a", "b"]
documents:
id: ["a", "b"] -> match
id: ["b", "a"] -> match
id: ["a"] -> no match
id: ["a", "b", "c"] -> no match
Eventually I want to use Java High Level REST Client to implement the query, though a example for elasticsearch dsl will do as well.
While this does not seem to be supported natively you could go ahead and use a script filter to achieve this behavior like so:
GET your_index/_search
{
"query": {
"bool": {
"must": [
{
"script": {
"script": "doc['tags'].values.length == 2"
}
},
{
"term": {
"tags": {
"value": "a"
}
}
},
{
"term": {
"tags": {
"value": "b"
}
}
}
]
}
}
}
The script filter limits the search result by the array size while the term filters specify the values of that array. Make sure to enable fielddata on the tags field in order to execute scripts on it.