21

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

2
  • You mean to avoid null values within your date range ? Commented Dec 14, 2016 at 10:05
  • From my understanding, he wants the search to return either the one with date grater than 2016-12-14 or have no deadline date associated to the document. Commented Sep 12, 2018 at 20:42

1 Answer 1

26

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.