Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine elasticsearch date range queries with null values

I would like to use date range query like the following :

{
    "range" : {
        "deadline" : {
            "gte" : "2016-12-14",
        }
    }
}

My index contains empty values for deadline as well. I would like to get those null dated documents in search results along with the dates in range. How can I combine date range with "must_not" exist query in elastic 5.x

like image 727
Mijoe Mathew Avatar asked Dec 14 '16 09:12

Mijoe Mathew


1 Answers

I think a bool query would do the trick.

{
  "query": {
    "bool": {
      "should": [
        {
          "range": {
            "deadline": {
              "gte": "2016-12-14"
            }
          }
        },
        {
          "bool": {
            "must_not": {
              "exists": {
                "field": "deadline"
              }
            }
          }
        }
      ]
    }
  }
}

In Elasticsearch indexes null values don't exist, so we use the exist query. Using the missing query would been less verbose, but it's deprecated since 2.2.

I don't have enough information so my example runs in query context, but maybe filter context would be more convenient in this case.

like image 118
Antonio Val Avatar answered Oct 21 '22 01:10

Antonio Val