Date Math in a Range Facet

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.
For more options, visit https://groups.google.com/groups/opt_out.

I had a similar requirement. It is possible to do this by using Date range aggregation. 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..! :slight_smile: