提问要多花一点心思哦

ES日期存储

Elasticsearch | 作者 sqyES | 发布于2017年12月15日 | 阅读数:6583

ES版本是5.5.3,文档版本是5.5档中关于日期格式默认是strings containing formatted dates, e.g. "2015-01-01" or "2015/01/01 12:10:30".但是在进行存储的时候"2015/01/01"或者是"2015/01/01 12:10:30"都有问题!错误信息是:"Invalid format: "2015/01/01" is malformed at "/01/01""
请问是什么原因?
已邀请:

napoay

赞同来自: wcl_24

 
是因为格式定义的不对哦,日期类型可以是任何你能想到的格式。比如,你想存储的日期格式是2015/01/01,那么在字段的mapping中设置日期格式为yyyy/MM/dd即可,其它格式也一样。命令如下:
 
DELETE testdate

PUT testdate
{
"mappings": {
"test":{
"properties": {
"datafield":{
"type": "date",
"format": "yyyy-MM-dd||yyyy/MM/dd||yyyy/MM/dd HH:mm:ss"
}
}
}
}
}
这里定义了datafield字段支持2015-01-01、2015/01/01、2015/01/01 12:10:30这三种格式。分别测试:
PUT testdate/test/1
{
"datafield":"2015-01-01"
}

PUT testdate/test/2
{
"datafield":"2015/01/01"
}

PUT testdate/test/3
{
"datafield":"2015/01/01 12:10:30"
}
都是OK的。
 
ps广告:欢迎关注我的新书哦,淘宝、京东搜索:从lucene到elasticsearch。

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自:

默认data类型,支持三种格式的,参见官网

wilbur

赞同来自:

推荐使用时间戳来存储时间

liucp

赞同来自:

传一个自定义时间字符串字段例如:customize_date:"2018-3-26 14:24:56"
 
怎么根据这个字段排序,感觉没法排序啊,是不是只能按照es内部时间排序哦
 

要回复问题请先登录注册