Elasticsearch按LocalDate.MAX搜索失败如何解决?

How to solve the Elasticsearch failing when searching by LocalDate.MAX?

我正在使用 Spring Data Elasticsearch 搜索 2010-01-01LocalDate.MAX 之间的内容,但失败了:

nested: ElasticsearchException[Elasticsearch exception [type=arithmetic_exception, reason=long overflow]]

springDataElasticsearchRepository.findByAgeBetween(startLocalDate, endLocalDate).

Elasticsearch 接受的最大日期是多少,或者应该如何避免此错误?

使用 springDataElasticsearchRepository.findByAgeBetween(Optional<LocalDate> startLocalDate, Optional<LocalDate> endLocalDate) 可以避免造成麻烦的 LocalDate.MINLocalDate.MAX

Elasticsearch 将日期存储为长值,表示自纪元以来的毫秒数。如果你运行下面的代码

public static void main(String[] args) {
    long maxLong = Long.MAX_VALUE;
    Instant instant = Instant.ofEpochMilli(maxLong);
    ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("UTC"));
    LocalDate localDate = LocalDate.from(zonedDateTime);
    System.out.println(localDate);
}

你会得到

+292278994-08-17

作为可表示的最大日期。最大 LocalDate 值为

+999999999-12-31