# Elasticsearch Date range query

**URL:** <https://discuss.elastic.co/t/elasticsearch-date-range-query/67117>\
**Category:** Elasticsearch\
**Created:** [November 24, 2016, 1:58pm UTC](https://discuss.elastic.co/t/elasticsearch-date-range-query/67117 "2016-11-24T13:58:37Z")\
**Posts on this page:** 3\
**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 24, 2016, 1:58pm UTC](https://discuss.elastic.co/t/elasticsearch-date-range-query/67117/1 "2016-11-24T13:58:37Z")

</div>

I am trying to execute following query. I have 3 attributes in document  
STATUS - Which can be "FAIL", "PASS" , "INVALID"  
DATE - contains date and time.

I want daily number of count for each status

> eg : Date : 11-09-2016, STATUS : FAIL, count: 120  
> Date : 11-09-2016, STATUS : PASS, count: 150

I want data for last one month, two month and so on

```
 SearchRequest requestQuery =
Requests.searchRequest(ConstantsValue.indexName)
    .types(ConstantsValue._Type)
    .source("{size:999999,"
    + "\"_source\" : "
    + "[\"DTCREATED\", \"STATUS\"]"				
    + ",\"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\" } }}}}}}}");

```

This code gives me daily count of each status but for all records. and want to add range filter something like below.

```
+"\"query\": {"
+" \"filtered\": {"
+" \"filter\": {"
+ "\"range\": { \"DTCREATED\": { \"gte\": \"now-90d/d\" }}"
+"}}}}}");

```

But I am not able to merge content of these two queries. I have tried my best. Any help is greatly appreciated.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [November 25, 2016, 3:02pm UTC](https://discuss.elastic.co/t/elasticsearch-date-range-query/67117/2 "2016-11-25T15:02:45Z")

</div>

You should put this query object at the same level as "aggs" in the previous request.

---

<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 23, 2016, 3:03pm UTC](https://discuss.elastic.co/t/elasticsearch-date-range-query/67117/3 "2016-12-23T15:03:23Z")

</div>

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