How to write a conditional query with Elasticsearch? -
i have simple json query elasticsearch looks this:
"query": { "bool": { "must": { "match": { "id": "1" }} , "must": { "match": { "tags.name": "a1"}} } }
how can execute second 'must' criteria if value ('a1' in case) not empty?
you can achieve using following -
{ "query": { "bool": { "must": [ { "match": { "id": "1" } }, { "bool": { "should": [ { "missing": { "field": "tags.name" } }, { "match": { "tags.name": "a1" } } ] } } ] } } }
Comments
Post a Comment