date - Elasticsearch现在可以解决问题

标签 date elasticsearch

我在尝试使查询与“现在”功能一起使用时遇到问题。我当前的查询看起来像这样:

{
     "query": {
        "bool" : {
            "must" : [ 
                { "match": { "originCountry" : "GB" }},
                { "match": { "destinationCity" : "MIL" }}
            ]
         }
    },
    "filter" : {
        "and": {
           "filters": [
              {
                  "exists":  {"field": "dateBack"}
              } ,
              {
                  "script" : {"script" : "doc['originRegion'].value == doc['destinationRegion'].value"}
              },
              {
                 "range": {
                    "dateOut": {
                       "gte": "now"
                    }
                 }
              }
           ]
        } 
    }
}

那没有返回任何结果。但是,如果我将范围部分更改为字符串日期,例如:
 "range": {
     "dateOut": {
             "gte": "20150101"
      }
 }

完美的作品。在我的索引映射中,所有日期字段都使用“basic_date”格式(YYYYMMDD)

这可能会给now函数带来任何问题吗?有谁知道now功能如何工作?是否将“现在”日期转换为要比较的字段使用的任何日期格式?我一直找不到有关此的任何有用的文档。
谢谢

最佳答案

检查您的日期映射-应该是YYYYMMdd而不是YYYYMMDD
设置映射时:

curl -XPOST http://localhost:9200/index/testnow/_mapping -d '
{"testnow": {
    "properties": { 
        "dateOut": {"type": "date","format" : "YYYYMMdd"}, 
        "dateBack": {"type": "date","format" : "YYYYMMdd"}
    }}}'  

并发布几个文档:
curl -XPOST http://localhost:9200/index/testnow/ -d '
{
    "originCountry": "GB",
    "destinationCity": "MIL",
    "dateBack" : "20140212",
    "originRegion" : "X",
    "destinationRegion" : "X",
    "dateOut" : "20140201"
}'

curl -XPOST http://localhost:9200/index/testnow/ -d '
{
    "originCountry": "GB",
    "destinationCity": "MIL",
    "dateBack" : "20150212",
    "originRegion" : "X",
    "destinationRegion" : "X",
    "dateOut" : "20150201"
}'

并运行查询:
curl -XGET http://localhost:9200/index/testnow/_search -d '
{
  "query" : {
    "filtered" : {
     "query": {
        "bool" : {
            "must" : [ 
                { "match": { "originCountry" : "GB" }},
                { "match": { "destinationCity" : "MIL" }}
            ]
         }
    },
    "filter" : {
      "and" : [
           {"exists":  {"field": "dateBack"}},
           {"script" : {"script" : "doc[\"originRegion\"].value == doc[\"destinationRegion\"].value"}},
           {"range": {"dateOut": {"gte": "now"}}}
          ]} }}}'

我按预期得到了一个文档:
    {
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.4142135,
    "hits" : [ {
      "_index" : "index",
      "_type" : "testnow",
      "_id" : "AUqgq8u4aqAGLvfmRnfz",
      "_score" : 1.4142135,
      "_source":
{
    "originCountry": "GB",
    "destinationCity": "MIL",
    "dateBack" : "20150212",
    "originRegion" : "X",
    "destinationRegion" : "X",
    "dateOut" : "20150201"
}
    } ]
  }
}

关于date - Elasticsearch现在可以解决问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27721711/

相关文章:

elasticsearch - ES : Bucket agg + top_hits + scroll? 如何返回桶中的所有命中(超过 `size+from` 最大值)?

php date() 没有收到年、月、日 : NSDate to php

javascript - GoJS highlightCollection 不适用于日期值

mysql - 从日期和时间值位于单独列中的表中选择

javascript - 奇怪的日期错误, "uncaught illegal access"

amazon-web-services - Cloudwatch 日志流式传输到 ElasticSearch AWS

php - Symfony2 表单 - 日期字段类型允许错误的日期

elasticsearch - Elasticsearch-使字段可聚合但不可搜索

elasticsearch - 在 ELK 中配置 number_of_shards 和 number_of_replicas

ElasticSearch:用匹配的搜索词标记文档