Elasticsearch: How to write an 'OR' clause in filter context? Elasticsearch: How to write an 'OR' clause in filter context? elasticsearch elasticsearch

Elasticsearch: How to write an 'OR' clause in filter context?


I had a similar scenario (even the range and match filter), with one more nested level, two conditions to be 'or'ed (as in your case) and another condition to be logically 'and'ed with its result. As @Pierre-Nicolas Mougel suggested in another answer I had nested bool clauses with one more level around the should clause.

{  "_source": [    "my_field"  ],  "query": {    "bool": {      "filter": {        "bool": {          "must": [            {              "bool": {                "should": [                  {                    "range": {                      "start": {                        "gt": "1558878457851",                        "lt": "1557998559147"                      }                    }                  },                  {                    "range": {                      "stop": {                        "gt": "1558898457851",                        "lt": "1558899559147"                      }                    }                  }                ]              }            },            {              "match": {                "my_id": "<My_Id>"              }            }          ],          "must_not": []        }      }    }  },  "from": 0,  "size": -1,  "sort": [],  "aggs": {}}

I read in the docs that minimum_should_match can be used too for forcing filter context. This might help you if this query doesn't work.


You need to wrap your should query in a filter query.

{     "query":{        "bool":{           "filter":[{              "bool":{                 "should":[                    {  // Query 1 },                  {  // Query 2 }               ]            }         }]      }   }}