Tldr;
You can achieve this with terms set query.
To understand
Example mapping:
{
"mappings": {
"properties": {
"name": {
"type": "keyword"
},
"myIndex": {
"type": "keyword"
},
"required_matches": {
"type": "long"
}
}
}
}
Example query:
{
"query": {
"terms_set": {
"myIndex": {
"terms": [3, 6, 9, 20],
"minimum_should_match_field": "required_matches"
}
}
}
}
In your case, required_matches
should be index as number of items of myIndex array.
To reproduce
Here is a fully working example:
First of all the setup:
DELETE /72004393/
PUT /72004393/
{
"mappings": {
"properties": {
"myIndex": {
"type": "keyword"
},
"name": {
"type": "text"
}
}
}
}
POST _bulk
{"index":{"_index":"72004393"}}
{"name":"WF1","myIndex":["3","4","5"]}
{"index":{"_index":"72004393"}}
{"name":"WF2","myIndex":["6","7","8"]}
{"index":{"_index":"72004393"}}
{"name":"WF3","myIndex":["9","10","11"]}
{"index":{"_index":"72004393"}}
{"name":"WF4","myIndex":["3","6","9"]}
The query:
GET /72004393/_search
{
"query": {
"terms_set": {
"myIndex": {
"terms": [ "3", "6", "9", "20" ],
"minimum_should_match_script": {
"source": "3"
}
}
}
}
}
Gives:
{
...
"hits" : {
...
"max_score" : 2.859232,
"hits" : [
{
"_index" : "72004393",
"_id" : "LqOQZ4ABOgujegeQ2gfV",
"_score" : 2.859232,
"_source" : {
"name" : "WF4",
"myIndex" : ["3","6","9"]
}
}
]
}
}
Another example:
PUT test
{
"mappings": {
"properties": {
"myIndex": {
"type": "keyword"
},
"name": {
"type": "text"
},
"required_matches": {
"type": "long"
}
}
}
}
POST _bulk
{"index":{"_index":"test"}}
{"name":"WF1","myIndex":["3","4","5"], "required_matches": 3}
{"index":{"_index":"test"}}
{"name":"WF2","myIndex":["6","7","8"], "required_matches": 3}
{"index":{"_index":"test"}}
{"name":"WF3","myIndex":["9","10","11"], "required_matches": 3}
{"index":{"_index":"test"}}
{"name":"WF4","myIndex":["3","6","9"], "required_matches": 3}
{"index":{"_index":"test"}}
{"name":"WF5","myIndex":["3","6","9", "15", "20"], "required_matches": 5}
{"index":{"_index":"test"}}
{"name":"WF6","myIndex":["3","6","9", "15"], "required_matches": 4}
Query:
GET test/_search
{
"query": {
"terms_set": {
"myIndex": {
"terms": ["3", "6", "9"],
"minimum_should_match_field": "required_matches"
}
}
}
}
The query above will match only WF4 because its required_match is 3 and WF5 AND WF6 required matches are 4 and 5.
If you update terms in the query to ["3", "6", "9", "15"]
it will match both WF4 AND WF5 and if you update it to ["3", "6", "9", "15", "20"]
it will match to WF4, WF5, WF6.