# Elasticsearch date range query using java api

**URL:** https://discuss.elastic.co/t/elasticsearch-date-range-query-using-java-api/67318
**Category:** Elasticsearch
**Created:** [November 28, 2016, 9:02am UTC](https://discuss.elastic.co/t/elasticsearch-date-range-query-using-java-api/67318 "2016-11-28T09:02:49Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![aniketpatil](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@aniketpatil](https://discuss.elastic.co/u/aniketpatil)
#### Post date: [November 28, 2016, 9:02am UTC](https://discuss.elastic.co/t/elasticsearch-date-range-query-using-java-api/67318/1 "2016-11-28T09:02:49Z")

</div>

I am using elasticsearch 1.5.2 and written query to get status count for last x days.

```
SearchRequest requestQuery =
 Requests.searchRequest(ConstantsValue.indexName)
.types(ConstantsValue._Type)
.source("{size:999999,"
 + "\"_source\" : "
 + "[\"DTCREATED\", \"STATUS\"]"
 +", \"query\": {"
 +" \"filtered\": {"
 +" \"filter\": {"
 + "\"range\": { \"DTCREATED\": { \"gte\": \"now-30d/d\" }}"
 +"}}}"
 + ",\"aggs\": "
 + "{\"group_by_STATUS\": {\"terms\": {\"field\": \"STATUS\"},"
 + "\"aggs\" : "
 + "{\"group_by_DATE\" : {\"date_histogram\" : "
 + "{\"field\" : \"DTCREATED\", \"interval\" : \"day\","
 + "\"format\" : \"yyyy-MM-dd\" },"
 + "\"aggs\" : "
 + "{\"grades_count\" : { \"value_count\" : { \"field\" : \"STATUS\" } }}}}}}}");

```

Now I want to use date range like data should be fetched within x to y date. How can I change above query to get expected result. DTCREATED field contains both date and time.

---

<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: [December 26, 2016, 9:03am UTC](https://discuss.elastic.co/t/elasticsearch-date-range-query-using-java-api/67318/2 "2016-12-26T09:03:05Z")

</div>

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