How to make Spring Data Elasticsearch work with java.time.LocalDateTime for date

前端 未结 2 923
你的背包
你的背包 2020-12-28 21:12

I am using Spring Data support for Elasticsearch. Here is the timestamp field mapping:

@Field(type = FieldType.Date, index = FieldIndex.not_analyzed, store =         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-28 21:52

    I was experiencing the similar issue: 'Z' in the date value is treated as a character, so the date parse failed. My solution is using custom pattern to make sure the conversion correct:

    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'")
    private LocalDateTime dateField;
    

    And if we don't want to repeat the pattern on different fields again and again, we can try to centralize to the conversion logic. Spring Data Elastic Search provide the custom conversion feature, check here for the example.

    Basically we can write a converter from String to LocalDateTime, and put the date pattern over there.

提交回复
热议问题