Using ZonedDateTime with Spring Boot and OpenSearch

118 views Asked by At

I'm using Spring Boot 3.1.2 and Spring Data Open Search Starter (spring-data-opensearch-starter) 1.2.0 library to save and read my entity from/to elastic search.

One of the field's type of my entity is ZonedDateTime:

@Builder
@Document(indexName = "orders", writeTypeHint = WriteTypeHint.FALSE)
public class OrderModel {
    @Id
    private String orderId;

    @Field(type = FieldType.Date, pattern = "uuuu-MM-dd'T'HH:mm:ss.SSS", format = {})
    private ZonedDateTime orderDate;

    private String store;
}

My repository extends ElasticsearchRepository:

@Repository
public interface OrderRepository extends ElasticsearchRepository<OrderModel, String> {
}

When I save my entity, I can see it is saved to elastic search succesfully:

"_index": "orders",
        "_id": "9202107589",
        "_score": 1,
        "_source": {
          "orderId": "9202107589",
          "placedDate": "2023-10-23T21:05:37.263",
          "store": “ZARA” 
         }

But, when I read it by calling orderRepository.existsById(orderId) it is throwing a conversion error below:

org.springframework.data.elasticsearch.core.convert.ConversionException: Unable to convert value '2023-10-23T21:05:37.263' to java.time.ZonedDateTime for property 'orderDate' at org.springframework.data.elasticsearch.core.convert.TemporalPropertyValueConverter.read(TemporalPropertyValueConverter.java:60)
    at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter$Reader.convertOnRead(MappingElasticsearchConverter.java:531)

Any idea to solve this problem?

Tried creating a custom converter but failed.

0

There are 0 answers