Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Date 弹性搜索函数问题_Date_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,Date,elasticsearch" /> elasticsearch,Date,elasticsearch" />

Date 弹性搜索函数问题

Date 弹性搜索函数问题,date,elasticsearch,Date,elasticsearch,我在尝试使用“now”函数获取查询时遇到问题。我当前的查询如下所示: { "query": { "bool" : { "must" : [ { "match": { "originCountry" : "GB" }}, { "match": { "destinationCity" : "MIL" }} ] } }, "fil

我在尝试使用“now”函数获取查询时遇到问题。我当前的查询如下所示:

{
     "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"
      }
 }
它工作得很好。在我的索引映射中,所有日期字段都使用“基本日期”格式(YYYYMMDD)

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

检查您的日期映射-它应该是
yyyyymmdd
而不是
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"
}
    } ]
  }
}

你现在的成绩是“大于或等于”吗?如果还没有被索引,你打算如何在将来得到一些东西?非常感谢Olly。对不起,完全是我的错,我相信所有的日期字段都配置正确,但不幸的是dateOut没有正确配置,它实际上配置为字符串,所以这就是为什么“现在”不起作用。我已经重新创建了映射,现在一切正常。抱歉给你添麻烦了