I want to write a simple query in ElasticSearch, that in pseudocode looks like this: WHERE X==A and (Y in (Y1, Y2) or Y is undefined)
. This is the query that I can come up with that works:
GET sales-staging/_search?filter_path=hits.hits._source
{
"query": {
"bool": {
"must": [
{
"term": {
"A": "X"
}
}
],
"should": [
{
"terms": {
"B": [
"Y1",
"Y2"
]
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "C"
}
}
]
}
}
],
"minimum_should_match": 1
}
}
}
I expect these documents to match:
{
"A": "X",
"B": "Y1"
},
{
"A": "X"
}
and these ones to not match:
{
"A": "X",
"B": "Y3"
},
{
"A": "Z",
"B": "Y1"
},
{
"A": "Z"
}
However, this is a quite hefty multiline query for such a simple query, and I have the feeling I am misusing the minimum_should_match
parameter to write a simple OR query. Am I overlooking a simpler way?
I looked at SO:
- Elasticsearch OR query
- Elasticsearch - "OR" query condition using match query and term query and the documentation
And on the documentation pages:
- https://www.elastic.co/guide/en/elasticsearch/guide/current/combining-filters.html
- https://www.elastic.co/guide/en/elasticsearch/guide/current/bool-query.html
But couldn't find the answer there either.