opensearch-java icon indicating copy to clipboard operation
opensearch-java copied to clipboard

[BUG] JsonException when SearchRequest contains a query of type range using .toJsonString function

Open mikeperello-scopely opened this issue 1 year ago • 3 comments

What is the bug?

The .toJsonString function throws a JsonException when a SearchRequest includes a query of type range.

Specifically, it shows the following error: Cannot find a serializer for type <type>. Consider using a full-featured JsonpMapper.

How can one reproduce the bug?

# Create a Query of type range
    Query rangeQuery = Query.of(q -> q.range(r -> r
            .field("time")
            .format("strict_date_optional_time")
            .gte(JsonData.of("2024-06-29T09:50:53Z"))
            .lte(JsonData.of("2024-06-29T10:50:53Z"))));
    
# Add it as part of a boolQuery
    BoolQuery boolQuery = BoolQuery.of(b -> b
            .filter(rangeQuery));

# Add the boolQuery to the SearchRequest
    SearchRequest searchRequest = new SearchRequest.Builder()
            .query(q -> q.bool(boolQuery))
            .build();

# Execute the toJsonString()
    System.out.println("query: " + searchRequest.toJsonString());

What is the expected behavior?

The query should print without any issues.

Do you have any additional context?

Other types of query, such as matchPhrase seem to work correctly. However, when adding a query of type range, it throws the error. I have been investigating the error, and it looks it's related to the parsing of the gte and lte elements. Although they are declared as JsonData types, the value is extracted as String (in this case), leading to the JsonException.

This, however, does not seem to affect the SearchRequest functionality, as I have been able to query sucessfully.

mikeperello-scopely avatar Aug 29 '24 14:08 mikeperello-scopely

I have this issue with all aggregation searches.

matthias-koch avatar Aug 30 '24 07:08 matthias-koch

@mikeperello-scopely @matthias-koch Would one of you please care to help writing a (failing) unit test for this? Let's fix it.

dblock avatar Sep 16 '24 16:09 dblock

Sure, @dblock! Added.

mikeperello-scopely avatar Sep 17 '24 10:09 mikeperello-scopely

The fix for this was released in v2.19.0 of the client

Xtansia avatar May 18 '25 22:05 Xtansia