0
votes

Is there a way to match all values in a document array? for eg. if my search array is ["1","2","3","4","5"] and my documents have fields like

doc1: "arr":["1","3","5"]

doc2: "arr":["1","2","7","9"]

doc3: "arr":["1","8"]

Then only the first document should be a match because all the values in the document are present in the search array. I tried using the script filter (to get the length of the array) and tried using the minimum_should_match parameter but I cant get it to work. How do I use a variable created by a script as a parameter for minimum_should_match?

1

1 Answers

0
votes

Can't directly search array to check whether contains. Because the analyzer will analysis the search key and match it, if there is any matched key, it will return results.

If want to match array whether contains the specified array, need to split the searched array to multiple terms, like:

{
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [{
                        "term": {
                            "number": 1
                        }
                    }, {
                        "term": {
                            "number": 2
                        }
                    }, {
                        "term": {
                            "number": 7
                        }
                    }, {
                        "term": {
                            "number": 9
                        }
                    }]
                }
            }
        }
    }
}