Date range java api examples

Hi, I am trying to search the documents using a date range filter along with some keywords. I couldn't find any java api samples to do so. Could you please help me.
Normal search works with the following code: But am after a date range filter search.

 SearchResponse<JsonNode> search = esClient.search(s -> s
                        .index(index)
                        .query(q -> q
                                .match(f -> f.field(searchKey).query(searchValue).fuzziness(fuzziness).operator(Operator.And))),
                JsonNode.class);

version details:

        <dependency>
            <groupId>co.elastic.clients</groupId>
            <artifactId>elasticsearch-java</artifactId>
            <version>7.15.2</version>
        </dependency>

Following code works. Please suggest if there is any better option.

        final String json = "{ \"fld_date\": {\"gte\": \"2018-02-02T15:08:28Z\", \"lte\": \"2021-06-23T11:35:01.120Z\"}}";
        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);
        SearchResponse<JsonNode> search = esClient.search(s -> s
                .index(index)
                .query( q -> q.range(value)), JsonNode.class);

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