LateCoder
LateCoder

Reputation: 2283

Match on a list of field value combinations in elasticsearch

I have a list of tuples of values, like this:

[("a1", "b1", "b1"), ("a2", "b2", "b3")... ("a_n", "b_n", "c_n")]

These are sets of values for fields "a", "b", and "c" that I'd like to use to match documents in elasticsearch. How can I do this type of multi-field terms query where matches must happen in sets as defined above?

Edit:

I tried using a bool query like so:

query={   
    "query": {
        "bool": {
            "should": [{
                    "bool": {
                        "must":[
                            {"term": {"a": a_val}},
                            {"term": {"b": b_val}},
                            {"term": {"c": c_val}}
                        ]
                    }
                }
                for (a_val, b_val, c_val) in identifiers
            ]
        },
    },
}

es.search('my_index', body=query)

but this resulted in:

TransportError(400, 'search_phase_execution_exception', 'failed to create query:...

I also tried a simpler version of the bool query but got the exact same error:

query = {   
    "query": {
        "bool": {
            "must": [
                {"term": {"a": "a1"}},
                {"term": {"b": "b1"}},
                {"term": {"c": "c1"}}     
            ]
        },
    },
}

es.search('my_index', body=query)

What am I doing wrong with these bool queries?

Upvotes: 1

Views: 588

Answers (1)

NikoNyrh
NikoNyrh

Reputation: 4138

If possible you should index them into a single keyword field with a separator like a1-b1-c1 etc. and then use a single terms filter with the array of values you are looking for.

Upvotes: 0

Related Questions