springboot集成elasticsearch7中Insta

2020-08-13  本文已影响0人  爱的旋转体

1 springboot集成elasticsearch7的过程中,遇到了以下问题:

Caused by: java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {MonthOfYear=8, YearOfEra=2020, DayOfMonth=13},ISO resolved to 07:36:42 of type java.time.format.Parsed
    at java.time.Instant.from(Instant.java:378)
    ... 192 common frames omitted
Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: InstantSeconds
    at java.time.format.Parsed.getLong(Parsed.java:203)
    at java.time.Instant.from(Instant.java:373)
    ... 192 common frames omitted

2 实体类中有Instant类型的属性:

   @Field(type = FieldType.Date,format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
   private Instant createdDate;

3 elaticsearch中配置的mapping:

        "createdDate": {
                "type": "date",
                "format": "yyyy-MM-dd HH:mm:ss"
            }

3 查阅相关文档

3.1 spring data elasticsearch

image.png

3.2 elasticsearch官网

image.png
image.png

4 解决办法

将java和elasticsearch中的日期格式统一改为uuuu-MM-dd'T'HH:mm:ss.SSSX

            "createdDate": {
                "type": "date",
                "format": "uuuu-MM-dd'T'HH:mm:ss.SSSX"
            }
@Field(type = FieldType.Date,format = DateFormat.custom, pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSX")
    private Instant createdDate;

参考:1.https://docs.spring.io/spring-data/elasticsearch/docs/4.0.3.RELEASE/reference/html/#elasticsearch.mapping.meta-model.rules
2.https://www.elastic.co/guide/en/elasticsearch/reference/current/migrate-to-java-time.html#java-time-migration-incompatible-date-formats

上一篇下一篇

猜你喜欢

热点阅读