Elasticsearch 查询 range 日期的问题

2017-08-28 14:30:44 +08:00
 hagezhou

现在用 range 查询日期总是查询不到,


比如我新建了一个 index curl -XPUT http://192.168.200.138:9200/company/


然后设置 mapping curl -XPUT http://192.168.200.138:9200/company/_mapping/employee -d ' { "properties" : { "date" : { "type" : "date", "format": "yyyy-MM-dd HH:mm:ss" }, "first_name" : { "type" : "string" }, "last_name" : { "type" : "string" }, "about" : { "type" : "string" }, "age" : { "type" : "long" } } }'


然后插入一条 curl -XPOST http://192.168.200.138:9200/company/employee/18 -d ' { "first_name" : "john", "last_name" : "smith", "age" : 25, "about" : "I love to go rock climbing", "interests": [ "sports", "music" ], "timestamp": "2017-08-28 14:15:30" }'


最后查询 curl -XGET 'http://192.168.200.138:9200/company/employee/_search' -d ' { "query" : { "range" : { "timestamp" : { "gt" : "now-2h" } } } } '

结果显示为空,这是为什么呢,timestamp 写得两点多,现在是两点半,时间肯定是两个小时以内,但就是查不出,有大神知道吗?多谢

4718 次点击
所在节点    Elasticsearch
2 条回复
hagezhou
2017-08-28 15:42:42 +08:00
已经解决了

首先是 mapping 的字段写得不对

其次是 es 默认的是 utc 时间,而服务器是 cst 时间,所有有了差距
dangyuluo
2017-08-29 00:16:53 +08:00
同一用时间戳,完美解决时区问题。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/386360

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX