弹性搜索 - 布尔过滤器:日期范围OR字段可以是nul [英] Elasticsearch - Bool filter: date range OR field can be nul

查看:118
本文介绍了弹性搜索 - 布尔过滤器:日期范围OR字段可以是nul的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在ElasticSearch中创建一个 bool过滤器,检查 start_date end_date 与今天相比。如果 start_date end_date 为null,则仍应返回结果。

I'm trying to create a bool filter in ElasticSearch that checks the start_date and end_date compared to today. If start_date or end_date is null, the result should still be returned.

所以例如今天是08-08-09。

So for example, today is 2016-08-09.


  • 项目1 :start_date:2016-08-04 end_date:2016-08-08 SHOULD不要退回

  • 项目2 :start_date:2016-08-08 end_date:2016-08-12 应该返回

  • 项目3 :start_date:null end_date:null 应该返回

  • Item 1: start_date: 2016-08-04 end_date: 2016-08-08 SHOULD NOT BE RETURNED
  • Item 2: start_date: 2016-08-08 end_date: 2016-08-12 SHOULD BE RETURNED
  • Item 3: start_date: null end_date: null SHOULD BE RETURNED

似乎无法使用我目前的代码:

Can't seem to get it to work with my current code:

POST _search
{
"query":{
    "filtered": {
        "query": {
            "match_all": {}
        },
        "filter" : {
            "bool": {
                "must": [{
                    "range": {
                        "start_date": {"lte": "2016-08-09"}         
                    }
                },
                {
                    "range": {
                        "end_date": {"gte": "2016-08-09"}     
                    }
                }],
                "should": [{
                    "missing": {
                        "field": "start_date"         
                    }
                },
                {
                    "missing": {
                        "field": "end_date"      
                    }
                }]
            }
        }
    }
}
}


推荐答案

你可能需要这样的东西,即:

You probably need something like this, i.e.:


  • start_date 必须为null或今天之前

  • end_date 必须为null或以后

  • start_date must either be null or before today
  • end_date must either be null or after today

修改查询:

POST _search
{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "bool": {
                "should": [
                  {
                    "range": {
                      "start_date": {
                        "lte": "now"
                      }
                    }
                  },
                  {
                    "missing": {
                      "field": "start_date"
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "should": [
                  {
                    "range": {
                      "end_date": {
                        "gte": "now"
                      }
                    }
                  },
                  {
                    "missing": {
                      "field": "end_date"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

这篇关于弹性搜索 - 布尔过滤器:日期范围OR字段可以是nul的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆