opensearch-project / opensearch-project/opensearch-java

[QUESTION] toJsonString() with Java 8 time module

Open
#1,730 1 comment 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.