# Dealing with an issue with Date Range filter

**URL:** <https://discuss.elastic.co/t/dealing-with-an-issue-with-date-range-filter/48623>\
**Category:** Elasticsearch\
**Created:** [April 28, 2016, 4:57am UTC](https://discuss.elastic.co/t/dealing-with-an-issue-with-date-range-filter/48623 "2016-04-28T04:57:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pawan\_Sharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pawan_sharma/32/9478_2.png) [@Pawan\_Sharma](https://discuss.elastic.co/u/Pawan_Sharma)\
**Post date:** [April 28, 2016, 4:57am UTC](https://discuss.elastic.co/t/dealing-with-an-issue-with-date-range-filter/48623/1 "2016-04-28T04:57:24Z")

</div>

I am working on Elastic Search and trying to fetch data by using aggregation queries from angularjs UI pages, we are using Elasticseach.js, Elastic.js etc plugins for

posting the request and getting the response, everything is working here for me except Date Range filters means Range filters is not working.  
Below is my query which i am using for posting a request:-

var filters = [ejs.RangeFilter('purchaseDate').gte(this.startDate).lte(this.endDate)];  
var composedFilter = ejs.AndFilter(filters);  
var myQuery = ejs.TermsAggregation('parentsku')  
.field(skufield.value)  
.order("\_term", "asc")  
.size(0)  
.agg(ejs.StatsAggregation('cost').field('directCost'))  
.agg(ejs.StatsAggregation('sales').field('revenu'))  
.agg(ejs.StatsAggregation('salesQuantity').field('salesQuantity'))  
.agg(ejs.StatsAggregation('quantity').field('quantity'))  
.agg(ejs.FilterAggregation('filtered').filter(composedFilter));

Here "purchaseDate" is the field in Elastic search Index on which i am trying to apply range filter.

Its json form of this is:-

{  
"aggs":{  
"parentsku":{  
"terms":{  
"field":"parentSku",  
"order":{  
"\_term":"asc"  
},  
"size":0  
},  
"aggs":{  
"cost":{  
"stats":{  
"field":"directCost"  
}  
},  
"sales":{  
"stats":{  
"field":"revenu"  
}  
},  
"salesQuantity":{  
"stats":{  
"field":"salesQuantity"  
}  
},  
"quantity":{  
"stats":{  
"field":"quantity"  
}  
},  
"filtered":{  
"filter":{  
"and":{  
"filters":[  
{  
"range":{  
"purchaseDate":{  
"gte":"2016-03-01",  
"lte":"2016-04-10"  
}  
}  
}  
]  
}  
}  
}  
}  
}  
}  
}

I need help if anyone can that would be appreciated.

---

<div class="post-metadata">

**Author:** ![Gui\_Keller](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gui_keller/32/9431_2.png) [@Gui\_Keller](https://discuss.elastic.co/u/Gui_Keller)\
**Post date:** [April 28, 2016, 5:19am UTC](https://discuss.elastic.co/t/dealing-with-an-issue-with-date-range-filter/48623/2 "2016-04-28T05:19:07Z")

</div>

Try the following filter:

```auto
{
	"filtered": {
		"filter": {
			"range": {
				"purchaseDate": {
					"gte": "2016-03-01",
					"lte": "2016-04-10"
				}
			}
		}
	}
}
```

Good luck! 🙂

---

<div class="post-metadata">

**Author:** ![Pawan\_Sharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pawan_sharma/32/9478_2.png) [@Pawan\_Sharma](https://discuss.elastic.co/u/Pawan_Sharma)\
**Post date:** [April 28, 2016, 5:35am UTC](https://discuss.elastic.co/t/dealing-with-an-issue-with-date-range-filter/48623/3 "2016-04-28T05:35:00Z")

</div>

Thanks Gui,  
I tried this it works but i need to select more values with a specific format that is because i am using aggregation kind of things...  
var filters = [ejs.RangeFilter('purchaseDate').gte(this.startDate).lte(this.endDate)];  
var composedFilter = ejs.AndFilter(filters);  
var myQuery = ejs.TermsAggregation('parentsku')  
.field(skufield.value)  
.order("\_term", "asc")  
.size(0)  
.agg(ejs.StatsAggregation('cost').field('directCost'))  
.agg(ejs.StatsAggregation('sales').field('revenu'))  
.agg(ejs.StatsAggregation('salesQuantity').field('salesQuantity'))  
.agg(ejs.StatsAggregation('quantity').field('quantity'))  
.agg(ejs.FilterAggregation('filtered').filter(composedFilter));

I think i am misplacing filter here it is not the problem of Date range although i am facing same issue with every attribute like Quantity, cost etc.  
Can you please help on that??

---

<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:** [July 5, 2017, 10:55pm UTC](https://discuss.elastic.co/t/dealing-with-an-issue-with-date-range-filter/48623/4 "2017-07-05T22:55:43Z")

</div>


