将elasticsearch日期范围查询与空值组合

Mij*_*hew 14 elasticsearch

我想使用日期范围查询,如下所示:

{
    "range" : {
        "deadline" : {
            "gte" : "2016-12-14",
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的索引也包含截止日期的空值.我想在搜索结果中获取那些null日期文档以及范围中的日期.如何将日期范围与弹性5.x中的"must_not"存在查询组合在一起

Ant*_*Val 17

我认为bool查询可以解决问题.

{
  "query": {
    "bool": {
      "should": [
        {
          "range": {
            "deadline": {
              "gte": "2016-12-14"
            }
          }
        },
        {
          "bool": {
            "must_not": {
              "exists": {
                "field": "deadline"
              }
            }
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在Elasticsearch索引中,null值不存在,因此我们使用exists查询.使用缺少的查询将不那么详细,但它自2.2以来已被弃用.

我没有足够的信息,因此我的示例在查询上下文中运行,但在这种情况,过滤器上下文可能会更方便.