V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
hagezhou
V2EX  ›  Elasticsearch

Elasticsearch 查询 range 日期的问题

  •  
  •   hagezhou · 2017-08-28 14:30:44 +08:00 · 4691 次点击
    这是一个创建于 2405 天前的主题,其中的信息可能已经有所发展或是发生改变。

    现在用 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 写得两点多,现在是两点半,时间肯定是两个小时以内,但就是查不出,有大神知道吗?多谢

    2 条回复    2017-08-29 00:16:53 +08:00
    hagezhou
        1
    hagezhou  
    OP
       2017-08-28 15:42:42 +08:00
    已经解决了

    首先是 mapping 的字段写得不对

    其次是 es 默认的是 utc 时间,而服务器是 cst 时间,所有有了差距
    dangyuluo
        2
    dangyuluo  
       2017-08-29 00:16:53 +08:00
    同一用时间戳,完美解决时区问题。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2916 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 11:14 · PVG 19:14 · LAX 04:14 · JFK 07:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.