【问题标题】:range between two dates in elastic Search弹性搜索中两个日期之间的范围
【发布时间】:2020-09-29 22:52:00
【问题描述】:

我对过滤器、范围和弹性搜索有疑问。

我在elasticSearch中有de文档

    {
      startDate : myDate
      endDate : onOtherDateOr Nothing

    }

我想搜索现在在 startDate 之后的范围,或者在 startDate 和 endDate 之间(如果定义了 endDate)。我怎样才能做到这一点 ?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您可以使用两个包含两个嵌套 bool/must 过滤器的 bool/should 过滤器来执行此操作:

    curl -XPOST localhost:9200/_search -d '{
      "query": {
        "filtered": {
          "filter": {
            "bool": {
              "should": [
                {
                  "bool": {
                    "must": [            <--- if endDate exists, check both bounds
                      {
                        "exists": {
                          "field": "endDate"
                        }
                      },
                      {
                        "range": {
                          "startDate": {
                            "lte": "now"
                          }
                        }
                      },
                      {
                        "range": {
                          "endDate": {
                            "gte": "now"
                          }
                        }
                      }
                    ]
                  }
                },
                {
                  "bool": {
                    "must": [            <--- if endDate missing, only check startDate
                      {
                        "missing": {
                          "field": "endDate"
                        }
                      },
                      {
                        "range": {
                          "startDate": {
                            "lte": "now"
                          }
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }
        }
      }
    }'
    

    【讨论】:

    • 当我使用这个查询时,弹性制造商 QueryParsingException,没有为应该定义过滤器,你能帮帮我吗
    • [{\"query\":{\"filtered\":{\"filter\":{\"geo_distance\":{\"distance\":\"1km\", \"geo\":{\"lat\":\"45.7677957\",\"lon\":\"4.8731638\"}},\"bool\":{\"应该\":[{\" bool\":{\"must\":[{\"missing\":{\"field\":\"enDate\"}},{\"range\":{\"startDate\":{\ "lte\":\"now\"}}}]}}]}}},\"sort\":{\"_geo_distance\":{\"geo\":{\"lat\":\ "45.7677957\",\"lon\":\"4.8731638\"},\"order\":\"asc\",\"unit\":\"km\",\"distance_type\":\" plane\"}}}]]] 这是我使用的请求
    • 请用你得到的QueryParsingException的堆栈跟踪更新你的问题,谢谢!
    【解决方案2】:

    我发现以下查询是最好的方法:

    {
      "query": {
        "range": {
          "order.dateCreated": {
            "dateFrom": "2011-01-24",
            "dateTo": "2011-01-24"
          }
        }
      }
    }
    

    【讨论】:

      【解决方案3】:

      尝试下一个查询,至少它对我有用。`

      "query": {
          "range": {
              "date_ field ": {  # this field_name should be the field where your date range 
                                              resides. 
                       "gte": startDate,
                        "lte": endDate,
                         "boost": 2.0
                            }
                          }
                        }`
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-09
        • 1970-01-01
        相关资源
        最近更新 更多