2013-03-26 138 views
15

我试图寻找所有谁有一个范围之内日期字段中的项目,它失败(返回任何结果)过滤按日期elasticsearch

查询:

{ 
    "query": { 
    "filtered": { 
     "query": { 
     "match_all": {} 
     }, 
     "filter": { 
     "range": { 
      "last_updated": { 
      "from": "2013-01-01 00:00:00" 
      } 
     } 
     } 
    } 
    } 
} 

的映射:

{ 
     took: 1 
     timed_out: false 
     _shards: { 
      total: 1 
      successful: 1 
      failed: 0 
     } 
     hits: { 
      total: 0 
      max_score: null 
      hits: [ ] 
     } 
    } 

{ 
    "my_doctype": { 
     "_timestamp": { 
      "enabled": "true" 
     }, 
     "properties": { 
      "cards": { 
       "type": "integer" 
      }, 
      "last_updated": { 
       "type": "date", 
       "format": "yyyy-MM-dd HH:mm:ss" 
      } 
     } 
    } 
} 

在结果

使用数值筛选整数字段(“卡”)的范围的相同查询可以正常工作。 将日期更改为较早的开始(1900-01-01 00:00:00)也不会显示结果。

我在做什么错?

顺便说一句,我知道我在映射中启用了_timestamp,但那不是我正在过滤的字段。

回答

26

看起来完全正常工作对我说:

curl -XPUT localhost:9200/test -d '{ 
    "settings": { 
     "index.number_of_shards": 1, 
     "index.number_of_replicas": 0 
    }, 
    "mappings": { 
     "doc": { 
      "_timestamp": { 
       "enabled": "true" 
      }, 
      "properties": { 
       "cards": { 
        "type": "integer" 
       }, 
       "last_updated": { 
        "type": "date", 
        "format": "yyyy-MM-dd HH:mm:ss" 
       } 
      } 
     } 
    } 
} 
' 
curl -XPOST localhost:9200/test/doc/1 -d '{ 
    "last_updated": "2012-01-01 12:13:14" 
} 
' 
curl -XPOST localhost:9200/test/doc/2 -d '{ 
    "last_updated": "2013-02-02 01:02:03" 
} 
' 
curl -X POST 'http://localhost:9200/test/_refresh' 
echo 
curl -X GET 'http://localhost:9200/test/doc/_search?pretty' -d '{ 
    "query": { 
     "filtered": { 
      "query": { 
       "match_all": {} 
      }, 
      "filter": { 
       "range": { 
        "last_updated": { 
         "gte": "2013-01-01 00:00:00" 
        } 
       } 
      } 
     } 
    } 
} 
' 
+1

谢谢。这是我的一个错字 - 多么尴尬。 我要在这里留下问题并标记为已接受,供参考,如果有人需要看到测试用例。或者删除它更有意义? – eran 2013-03-26 18:55:39

+14

@eran第四次在谷歌搜索“elasticsearch日期搜索(至少对我来说)。保留它,这对我很有用=) – markdsievers 2013-10-15 04:10:17

+0

@Gil你可以更具体一些吗? – imotov 2014-03-04 15:33:39