Java Api Client RangeQueryBuilder has JsonData as an attribute for gte etc

Hello.
I am trying to migrate my java application to use new java api client.
Today I realized that RangeQUeryBuildar has these strange types for gte, lte, etc methods.
Why is it JsonData type?

I am using Elasticsearch-java-7.16.2 version

Regards
Piotr

2 Likes

Range query accepts a number of different types such as string, numbers, date, date math, etc. The Java API client doesn't yet support all these types, and these values have been implemented as raw json data.

This will be improved in a future release. In the meantime, you can pass e.g. a string value (also works for other primitive types) as:

RangeQuery rq = RangeQuery.of(q -> q.gt(JsonData.of("foo")));

Hope this helps.

2 Likes

I am using the version 7.15.2 and couldn't figure out how to use RangeQueryBuilder. Currently I am creating a JsonValue object and passing it to Query.Builder 's range method as below:

        final String json = "{ \"fld_date\": {\"gte\": \"2018-02-02T15:08:28Z\", \"lte\": \"2021-06-19T16:03:01.130Z\"}}";
        final JsonpMapper mapper = new JsonbJsonpMapper();
        final JsonParser parser = mapper.jsonProvider().createParser(new StringReader(json));
        final JsonData data = JsonData.from(parser, mapper);
        final JsonValue value = data.toJson(mapper);
 Query.Builder rangeBuilder = new Query.Builder();
rangeBuilder.range(value );

See the post:
https://discuss.elastic.co/t/date-range-java-api-examples/295505/2

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.