0

How would I approach the following problem: I want to filter on a field which contains multiple values(eg. ["value1", "value2", "value3"]).

The filter would also contain multiple values (eg. ["value1", "value2"]. I want to get back only the items which have the same field value as filter, eg. field is ["value1", "value2"] and the filter is also ["value1", "value2"]

Any help would be greatly appreciated

1

1 Answer 1

1

I think the somewhat-recently added (v6.1) terms_set query (which Val references on the question he linked in his comment) is what you want.

terms_set, unlike a regular terms, has a parameter to specify a minimum number of matches that must exist between the search terms and the terms contained in the field.

Given:

PUT my_index/_doc/1
{ 
    "values": ["living", "in a van", "down by the river"],
}
PUT my_index/_doc/2
{
    "values": ["living", "in a house", "down by the river"],
}

A terms query for ["living", "in a van", "down by the river"] will return you both docs: no good. A terms_set configured to require all three matching terms (the script params.num_terms evaluates to 3) can give you just the matching one:

GET my_index/_search
{
    "query": { 
        "terms_set": {
            "values": {
                "terms": ["living", "in a van", "down by the river"],
                "minimum_should_match_script": {
                  "source": "params.num_terms"
                }
            }            
        }
    }
}

NOTE: While I used minimum_should_match_script in the above example, it isn't a very efficient pattern. The alternative minimum_should_match_field is the better approach, but using it in the example would have meant a couple of more PUTs to add the necessary field to the documents, so I went with brevity.

Sign up to request clarification or add additional context in comments.

1 Comment

This helped a lot, only problem is we are currently below version 6.1 so the implementation will have to wait until we upgrade. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.