1

I'm using spring-data-elasticsearch 4.0.1 and elastic cluster 7.6, when I define an attribute with a custom pattern with "yyyy-MM-dd" and try to retrieve a date with value "2014-06-11" it throws an error.

@Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd")
private Date startDate;

Error:

java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {},ISO resolved to 2014-06-11 of type java.time.format.Parsed

I tried with this also but get an error again:

@Field(type = FieldType.Date, format = DateFormat.date_optional_time)
private Date startDate;

I read in the documentation that I should use the pattern "uuuu-MM-dd" for elastic 7 version, but this doesn't work either.

1
  • 2
    It’s not my home field, so just a guess: try using LocalDate instead of Date. Commented Nov 20, 2020 at 7:52

3 Answers 3

1

Elasticsearch 7 uses the modern date-time API. Given below is an excerpt from https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#reference

Properties that derive from TemporalAccessor must either have a @Field annotation of type FieldType.Date or a custom converter must be registered for this type. If you are using a custom date format, you need to use uuuu for the year instead of yyyy. This is due to a change in Elasticsearch 7.

Change you annotation and type as follows:

@Field(type = FieldType.Date, format = DateFormat.date)
private LocalDate startDate;

Learn more about the modern date-time API at Trail: Date Time.

Some more references:

  1. Elasticsearch Built In Formats for date-time
  2. DateTimeFormatter
Sign up to request clarification or add additional context in comments.

Comments

1

A java.util.Date is not a plain date consisting of a year,month and day, but an instant in time in the UTC timezone. There is no way to convert "2014-06-11" to an instant in time. What hours and minutes should be used? In what timezone?

Like Ole wrote in the comment, use java.time.LocalDate for this. This class is for exactly that use case: a year with a month and a day. And please stop using the old java.util.Date class. Since Java 8, there are the classes in the java.time package.

Comments

0

This issue has been resolved By Spring Data Elasticsearch 4.0.5. Spring Boot 2.3.5 use Spring Data Elasticsearch 4.0.5. (Spring Boot 2.3.5 available Oct 29.2020.)

https://github.com/spring-projects/spring-data-elasticsearch/pull/538 DATAES-953 - DateTimeException on converting Instant or Date to custo…

https://github.com/spring-projects/spring-data-elasticsearch/releases/tag/4.0.5.RELEASE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.