opensearch-project / opensearch-project/opensearch-java
[QUESTION] toJsonString() with Java 8 time module
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 165
- Forks
- 250
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 26
Description
Hi,
I don't seem to be able to serialise SearchRequest(for example) if i am using OffsetDateTime in my range query.
For example:
OffsetDateTime from, OffsetDateTime to;
BoolQuery.Builder bool = QueryBuilders.bool();
bool.filter(QueryBuilders.range().field("ts").from(JsonData.of(from)).to(JsonData.of(to)).build().toQuery());
SearchRequest searchRequest = new SearchRequest.Builder()
.size(10000)
.query(bool.build().toQuery())
.index(Arrays.stream(indices).toList()).build();
System.out.println(searchRequest.toJsonString());// This is where the error is
The error is
jakarta.json.JsonException: Cannot find a serializer for type java.time.OffsetDateTime. Consider using a full-featured JsonpMapper.
This is how i init my Opensearch client and setting the mapper which has the timeModule
private OpenSearchClient getClientInternal() {
final HttpHost host = new HttpHost("https",hostname);
final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(host), new UsernamePasswordCredentials(username, password.toCharArray()));
JsonpMapper mapper = new JacksonJsonpMapper(jacksonObjectMapper);
var transport = ApacheHttpClient5TransportBuilder.builder(host)
.setMapper(mapper) //**Set the mapper here which has the timeModule**
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
)
.build();
return new OpenSearchClient(transport);
}
My ObjectMapper config
@Bean
public ObjectMapper jacksonObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
System.out.println("Auto-configured Jackson ObjectMapper bean created. "+ mapper.hashCode());
return mapper;
}
I don't see anything wrong, but i still get the error.
Any ideas ?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at SearchRequest.toJsonString() and the JacksonJsonpMapper setup shown in the issue, then trace which mapper serializes JsonData values. Reproduce the OffsetDateTime range-query example and compare the mapper used by request serialization with the configured ObjectMapper; done means identifying a supported serialization path or documenting the required configuration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100