在
创建index
的之后,可以设置mapping
。如果mapping
中没有设置date的format,那么默认为两种格式:
strict_date_optional_time
epoch_millis
其中新版strict_date_optional_time
移除了前缀strict_
。
(1)date_optional_time
此格式为ISO8601标准 示例:2019-08-20T14:56:18.000+08:00
(2)epoch_millis
也就是时间戳 示例:1515150699465
以上2中是默认的
其他的一些格式参考官网https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html
在spring data中如何实现
(1)创建文档时指定格式
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-dd'T'HH:mm:ss.SSSZ",timezone="GMT+8")
private Date callback_at;
如下采用第一种方式:
@Field(type = FieldType.Date,format = DateFormat.custom,pattern ="yyyy-MM-dd HH:mm:ss")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date createTime;
@Field(type = FieldType.Date,format = DateFormat.custom,pattern ="yyyy-MM-dd HH:mm:ss")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date updateTime;