Elasticsearch 插入时间字段时数据格式问题

elasticsearch 时间格式

elasticsearch创建index的之后,可以设置mapping。 如果mapping中没有设置date的format,那么默认为两种格式 strict_date_optional_time||epoch_millis ,其中新版strict_date_optional_time移除了前缀strict_。

    date_optional_time 此格式为 示例:2018-08-31T14:56:18.000+08:00 epoch_millis 也就是时间戳 示例1515150699465, 1515150699

以上2中是默认的,其他的格式参考

数据插入

进入默认的格式化格式是以上两种,那么只用传入对应的格式即可,想使用哪种格式的时间,就先设置对应field的format。

  1. 设置格式 PUT my_index { "mappings": { "_doc": { "properties": { "date": { "type": "date", "format": "yyyy-MM-dd" } } } } }
  2. 转换java类的属性 jackson库在转换为json的时候,将Date类型转为为了long型的字符串表示。我们使用@JsonFormat注解改变格式化的结果 @Field(type = FieldType.Date, format = DateFormat.custom,pattern = "yyyy-MM-dd") @JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd",timezone="GMT+8") private Date callback_at; 如果是默认的date_optional_time 的格式 @Field(type = FieldType.Date, format = DateFormat.date_optional_time) @JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-ddTHH:mm:ss.SSSZ",timezone="GMT+8") private Date callback_at;

其他格式举一反三即可

参考文档:

elasticsearch 时间格式 elasticsearch创建index的之后,可以设置mapping。 如果mapping中没有设置date的format,那么默认为两种格式 strict_date_optional_time||epoch_millis ,其中新版strict_date_optional_time移除了前缀strict_。 date_optional_time 此格式为 示例:2018-08-31T14:56:18.000+08:00 epoch_millis 也就是时间戳 示例1515150699465, 1515150699 以上2中是默认的,其他的格式参考 数据插入 进入默认的格式化格式是以上两种,那么只用传入对应的格式即可,想使用哪种格式的时间,就先设置对应field的format。 设置格式 PUT my_index { "mappings": { "_doc": { "properties": { "date": { "type": "date", "format": "yyyy-MM-dd" } } } } } 转换java类的属性 jackson库在转换为json的时候,将Date类型转为为了long型的字符串表示。我们使用@JsonFormat注解改变格式化的结果 @Field(type = FieldType.Date, format = DateFormat.custom,pattern = "yyyy-MM-dd") @JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd",timezone="GMT+8") private Date callback_at; 如果是默认的date_optional_time 的格式 @Field(type = FieldType.Date, format = DateFormat.date_optional_time) @JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-ddTHH:mm:ss.SSSZ",timezone="GMT+8") private Date callback_at; 其他格式举一反三即可 参考文档:
经验分享 程序员 微信小程序 职场和发展