【问题标题】:Elastic search exact range for date弹性搜索日期的确切范围
【发布时间】:2016-04-14 19:03:20
【问题描述】:

尝试使用范围查询进行“精确”日期匹配但没有成功。 我有下面映射的agreement_start_date 字段

"agreement_start_date" : {
     "type" : "date",
     "format" : "strict_date_optional_time||epoch_millis"
} 

我已为单个文档编制索引,并且似乎已编制索引。

curl "localhost:9200/cr/_search?pretty=true" -d ''

{
  "took" : 35,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "cr",
      "_type" : "agreement_index",
      "_id" : "570fe59b7f9e6f7f762129ed",
      "_score" : 1.0,
      "_source":{"agreement_start_date": "2016-04-14T18:46:51.268176+00:00"} 
  }
}

我想通过将agreement_start_date 与特定日期匹配的简单查询来获取此记录。

{
  "query": {
    "bool": {
      "must": {
        "range": {
          "agreement_start_date": {
            "gte": "2016-04-14",
            "lte": "2016-04-14",
            "format": "yyyy-MM-dd"
          }
        }
      }
    }
  }
}

此查询返回 0 个结果。如何在弹性搜索中进行查询,只比较提供的日期部分?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    似乎将日期四舍五入到最近的一天就可以了。

    {
      "query": {
        "bool": {
          "must": {
            "range": {
              "agreement_start_date": {
                "gte": "2016-04-14||\d",
                "lte": "2016-04-14||\d",
                "format": "yyyy-MM-dd"
              }
            }
          }
        }
      }
    }
    

    此查询返回索引文档。

    【讨论】:

      【解决方案2】:

      我用过这个:

      "range" : {
          "agreement_start_date" : {
              "gte": "2016-04-14",
              "lte": "2016-04-14",
              "format": "yyyy-MM-dd||yyyy-MM-dd",
              "time_zone": "Asia/Seoul"
          }
      }
      

      【讨论】:

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