# Date Math in a Range Facet

**URL:** https://discuss.elastic.co/t/date-math-in-a-range-facet/10977
**Category:** Elasticsearch
**Created:** [March 2, 2013, 8:50pm UTC](https://discuss.elastic.co/t/date-math-in-a-range-facet/10977 "2013-03-02T20:50:45Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Wes\_Plunk\_2](https://avatars.discourse-cdn.com/v4/letter/w/977dab/32.png) [@Wes\_Plunk\_2](https://discuss.elastic.co/u/Wes_Plunk_2)
#### Post date: [March 2, 2013, 8:50pm UTC](https://discuss.elastic.co/t/date-math-in-a-range-facet/10977/1 "2013-03-02T20:50:45Z")

</div>

Does anyone know if it's possible to do Date Math in a Range Facet

I need a facet that would be a range, like this:

{

"range": {

"dateField1":[

{ "from": "now", "to": "now+1d" }

,{"from": "now-1d", "to": "now" }

,{"from": "now-1w", "to": "now+1d" }

,{"from": "now-1m", "to": "now+1d" }

,{"from": "now-2m", "to": "now+1d" }

]

}

}

The result should be a facet with values like:

Date Field Facet

Today

Yesterday

Within the Last Week

Within the Last Month

Within the Last 2 Months

If it's not possible, does anyone know how you can do this without having  
to dynamically change the facet definition every time it runs

Thanks  
Wes

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![krati](https://avatars.discourse-cdn.com/v4/letter/k/ac91a4/32.png) [@krati](https://discuss.elastic.co/u/krati)
#### Post date: [August 10, 2016, 8:28am UTC](https://discuss.elastic.co/t/date-math-in-a-range-facet/10977/2 "2016-08-10T08:28:37Z")

</div>

I had a similar requirement. It is possible to do this by using [Date range aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html). Following is the query which I used for getting the results:

{  
"aggs": {  
"er\_date\_range": {  
"date\_range": {  
"field": "createdOn",  
"format": "yyy-MM-dd HH:mm:ss.SSS",  
"ranges": [  
{  
"key": "Past 24 Hours",  
"from": "now-1d"  
},  
{  
"key": "Past Week",  
"from": "now/d-1w"  
},  
{  
"key": "Past Month",  
"from": "now/d-1M"  
},  
{  
"key": "Past 6 Months",  
"from": "now/d-6M"  
},  
{  
"key": "Past Year",  
"from": "now/d-1y"  
},  
{  
"key": "Past 3 Years",  
"from": "now/d-3y"  
},  
{  
"key": "Earlier",  
"to": "now/d-3y"  
}  
]  
}  
}  
},  
"size": 0  
}

Hope this helps..! 🙂

---

<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:28pm UTC](https://discuss.elastic.co/t/date-math-in-a-range-facet/10977/3 "2017-07-05T22:28:51Z")

</div>


