如何解决按LocalDate搜索时Elasticsearch失败的问题.MAX



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

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

具有springDataElasticsearchRepository.findByAgeBetween(startLocalDate, endLocalDate)

Elasticsearch接受的最长日期是什么?否则应该如何避免此错误?

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

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

使用springDataElasticsearchRepository.findByAgeBetween(Optional<LocalDate> startLocalDate, Optional<LocalDate> endLocalDate)可以避免引起故障的LocalDate.MINLocalDate.MAX

最新更新