在ElasticSearch中搜索多个日期字段

时间:2018-12-11 20:30:01

标签: elasticsearch

我有一个这样的文件:

{
  "listings": {
    "mappings": {
      "listing": {
        "properties": {
          "auctionOn": {
            "type": "date"
          },
          "inspections": {
            "type": "nested",
            "properties": {
              "endsOn": {
                "type": "date"
              },
              "startsOn": {
                "type": "date"
              }
            }
          },
        // more fields.. snipped for brevity
        }
      }
    }
  }
}

并且我想执行以下搜索:(需要是布尔过滤器。.无需评分)

  • 返回任何检查的文件.startsOn与提供的任何日期匹配(如果提供的话) 或
  • 返回文件,其中auctionOn匹配提供的日期(如果提供) 他们还可以指定搜索a)仅检查,b)仅拍卖。如果未提供,则两个日期都必须匹配。

换句话说,可能的搜索:

  • 在有检查/拍卖的地方搜索
  • 搜索要检查的地方
  • 搜索有拍卖的地方
  • 搜索在提供的日期是否有检查/拍卖的地方
  • 搜索在提供的日期是否有检查的地方
  • 搜索在提供的日期有拍卖的地方

现在,我已经在布尔查询过滤器中:

{
   "query":
   {
      "bool":
      {
          "filter":[{"terms":{"location.suburb":["Camden"]}}
      }
   }
}

,我需要将此新过滤器分开。所以..这就像是主布尔过滤器中的嵌套过滤器?

因此,如果提供了“郊区=卡姆登,日期= ['2018-11-01','2018-11-02']'

然后应返回郊区= Camden的文件,并且检查或拍卖日期均包括所提供的日期之一。

我对如何做感到很困惑,所以任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

针对您在问题中提到的案例,将有很多布尔查询组合。以您提到的示例为例,

  

因此,如果提供了“郊区=卡姆登,日期= ['2018-11-01','2018-11-02']'

     

然后,它应该返回郊区= Camden且其中一个   检查或拍卖日期包括提供的日期之一。

假设您的位置过滤器按预期工作,例如上述日期中的日期该查询的附加内容将是:

{
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "location.suburb": [
              "Camden"
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "terms": {
                  "auctionOn": [
                    "2018-11-01",
                    "2018-11-02"
                  ]
                }
              },
              {
                "nested": {
                  "path": "inspections",
                  "query": {
                    "bool": {
                      "should": [
                        {
                          "terms": {
                            "inspections.startsOn": [
                              "2018-11-01",
                              "2018-11-02"
                            ]
                          }
                        },
                        {
                          "terms": {
                            "inspections.endsOn": [
                              "2018-11-01",
                              "2018-11-02"
                            ]
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
相关问题