资讯专栏INFORMATION COLUMN

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

yeyan1996 / 2420人阅读

摘要:时间格式创建的之后,可以设置。如果中没有设置的,那么默认为两种格式其中新版移除了前缀。设置格式转换类的属性库在转换为的时候,将类型转为为了型的字符串表示。

elasticsearch 时间格式

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

date_optional_time 此格式为ISO8601标准 示例:2018-08-31T14:56:18.000+08:00

epoch_millis 也就是时间戳 示例1515150699465, 1515150699

以上2中是默认的,其他的格式参考官方文档-format

数据插入

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

设置格式

</>复制代码

  1. PUT my_index
  2. {
  3. "mappings": {
  4. "_doc": {
  5. "properties": {
  6. "date": {
  7. "type": "date",
  8. "format": "yyyy-MM-dd"
  9. }
  10. }
  11. }
  12. }
  13. }

转换java类的属性

jackson库在转换为json的时候,将Date类型转为为了long型的字符串表示。我们使用@JsonFormat注解改变格式化的结果

</>复制代码

  1. @Field(type = FieldType.Date, format = DateFormat.custom,pattern = "yyyy-MM-dd")
  2. @JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd",timezone="GMT+8")
  3. private Date callback_at;

如果是默认的date_optional_time 的格式

</>复制代码

  1. @Field(type = FieldType.Date, format = DateFormat.date_optional_time)
  2. @JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd"T"HH:mm:ss.SSSZ",timezone="GMT+8")
  3. private Date callback_at;

其他格式举一反三即可

参考文档:
springboot elasticsearch 集成注意事项

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77008.html

相关文章

发表评论

0条评论

最新活动
阅读需要支付1元查看
<