Java: 1.8
Spring Boot: 2.3.2.RELEASE
Elasticsearch: 7.9.2
当实体类设置日期类型为Date
时,保存数据没有问题,但查询时报错:
java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {},ISO resolved to 2007-08-09 of type java.time.format.Parsed
at java.time.Instant.from(Instant.java:378)
at org.springframework.data.elasticsearch.core.convert.ElasticsearchDateConverter.parse(ElasticsearchDateConverter.java:125)
at org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchPersistentProperty$1.read(SimpleElasticsearchPersistentProperty.java:160)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.readValue(MappingElasticsearchConverter.java:281)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter$ElasticsearchPropertyValueProvider.getPropertyValue(MappingElasticsearchConverter.java:869)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.readProperties(MappingElasticsearchConverter.java:260)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.readEntity(MappingElasticsearchConverter.java:192)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.read(MappingElasticsearchConverter.java:172)
at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.read(MappingElasticsearchConverter.java:81)
at org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate$ReadDocumentCallback.doWith(AbstractElasticsearchTemplate.java:602)
at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.get(ElasticsearchRestTemplate.java:166)
使用java.time
包下的日期类型替代Date
@Field(type = FieldType.Date, format =
DateFormat.year_month_day)
时,类型要为LocalDate
;
@Field(type = FieldType.Date, format =
DateFormat.date_hour_minute_second_millis)
时,类型要为LocalDateTime
;