Search code examples
elasticsearchspring-data-elasticsearch

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


I'm using Spring Data Elasticsearch to search for something between 2010-01-01 and LocalDate.MAX which fails with:

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

Having springDataElasticsearchRepository.findByAgeBetween(startLocalDate, endLocalDate).

What is the maximum date Elasticsearch accepts or how should otherwise avoid this error?


Solution

  • Elasticsearch stores dates as long values representing milliseconds since epoch. If you run the following code

    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);
    }
    

    you will get

    +292278994-08-17
    

    as the maximum date that is representable. The maximum LocalDate value is

    +999999999-12-31