TotalTard
TotalTard

Reputation: 45

Filtering Field with multiple values

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

Upvotes: 0

Views: 437

Answers (1)

mike b
mike b

Reputation: 514

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.

Upvotes: 1

Related Questions