Description
Hello,
I'm implementing the new Java client for elasticsearch on my microservice and I'm stuck because it's impossible to configure the JacksonJsonpMapper to add the java time module inside.
This mapper is created in the class ElasticsearchClients.java line 273 by instancing a default JacksonJsonpMapper:
return new RestClientTransport(restClient, new JacksonJsonpMapper(), transportOptionsWithHeader);
It makes it impossible to change the implementation using a bean inside the elasticsearch client configuration. Moreover, the class ElasticsearchClientConfigurations from spring boot auto configure package provide a bean JacksonJsonpMapper to handle this use case. This mapper should use the bean from the autoconfig and not an instantiated object.
Thus when doing a query like this one where endDate is an Instant:
Query query = NativeQuery.builder()
.withQuery(q -> q.bool(b -> b
.must(m ->m.term(t -> t.field(TENANT_KEY).value(tenantId)))
.filter(f -> f.range(r -> r.field(DATE_KEY).lte(JsonData.of(endDate))))))
.withAggregation(STORAGE, Aggregation.of(a -> a.sum(v -> v.field(SIZE_KEY))))
.withMaxResults(0)
.build();
We have an error because the mapper hasn't registered the JavaTimeModule:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.Instant` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1306)
at com.fasterxml.jackson.databind.ser.impl.UnsupportedTypeSerializer.serialize(UnsupportedTypeSerializer.java:35)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:3208)
at co.elastic.clients.json.jackson.JacksonJsonpMapper.serialize(JacksonJsonpMapper.java:98)
at co.elastic.clients.json.JsonDataImpl.serialize(JsonDataImpl.java:100)
at co.elastic.clients.elasticsearch._types.query_dsl.RangeQuery.serializeInternal(RangeQuery.java:197)
at co.elastic.clients.elasticsearch._types.query_dsl.QueryBase.serialize(QueryBase.java:86)
at co.elastic.clients.elasticsearch._types.query_dsl.Query.serialize(Query.java:1200)
at co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery.serializeInternal(BoolQuery.java:128)
at co.elastic.clients.elasticsearch._types.query_dsl.QueryBase.serialize(QueryBase.java:86)
at co.elastic.clients.elasticsearch._types.query_dsl.Query.serialize(Query.java:1200)
at co.elastic.clients.elasticsearch.core.SearchRequest.serializeInternal(SearchRequest.java:976)
at co.elastic.clients.elasticsearch.core.SearchRequest.serialize(SearchRequest.java:858)
at co.elastic.clients.json.JsonpMapperBase$JsonpSerializableSerializer.serialize(JsonpMapperBase.java:111)
at co.elastic.clients.json.JsonpMapperBase$JsonpSerializableSerializer.serialize(JsonpMapperBase.java:108)
at co.elastic.clients.json.jackson.JacksonJsonpMapper.serialize(JacksonJsonpMapper.java:92)
at co.elastic.clients.transport.rest_client.RestClientTransport.prepareLowLevelRequest(RestClientTransport.java:215)
at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:146)
at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1518)
at org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate.lambda$search$14(ElasticsearchTemplate.java:323)
at org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate.execute(ElasticsearchTemplate.java:538)
at org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate.search(ElasticsearchTemplate.java:323)
at org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate.search(AbstractElasticsearchTemplate.java:476)