# Date range java api examples

**URL:** <https://discuss.elastic.co/t/date-range-java-api-examples/295505>\
**Category:** Elasticsearch\
**Created:** [January 26, 2022, 8:18pm UTC](https://discuss.elastic.co/t/date-range-java-api-examples/295505 "2022-01-26T20:18:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nandan-g](https://avatars.discourse-cdn.com/v4/letter/n/d2c977/32.png) [@nandan-g](https://discuss.elastic.co/u/nandan-g)\
**Post date:** [January 26, 2022, 8:18pm UTC](https://discuss.elastic.co/t/date-range-java-api-examples/295505/1 "2022-01-26T20:18:00Z")

</div>

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.

```auto
 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:

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

```

---

<div class="post-metadata">

**Author:** ![nandan-g](https://avatars.discourse-cdn.com/v4/letter/n/d2c977/32.png) [@nandan-g](https://discuss.elastic.co/u/nandan-g)\
**Post date:** [January 27, 2022, 8:02pm UTC](https://discuss.elastic.co/t/date-range-java-api-examples/295505/2 "2022-01-27T20:02:33Z")

</div>

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

```auto
        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);

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 24, 2022, 8:03pm UTC](https://discuss.elastic.co/t/date-range-java-api-examples/295505/3 "2022-02-24T20:03:34Z")

</div>

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