# Range facet with dates computing?

**URL:** <https://discuss.elastic.co/t/range-facet-with-dates-computing/11494>\
**Category:** Elasticsearch\
**Created:** [April 8, 2013, 11:07pm UTC](https://discuss.elastic.co/t/range-facet-with-dates-computing/11494 "2013-04-08T23:07:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erwin\_Rijss](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/erwin_rijss/32/955_2.png) [@Erwin\_Rijss](https://discuss.elastic.co/u/Erwin_Rijss)\
**Post date:** [April 8, 2013, 11:07pm UTC](https://discuss.elastic.co/t/range-facet-with-dates-computing/11494/1 "2013-04-08T23:07:42Z")

</div>

Hi All,

I like to create a facet list like:

- this week
- last month
- last quarter
- last year
- older

I have a dataset with an adddate field. The adddate in milliseconds minus  
the current date/time in millisecons gives me an amount of milliseconds  
which I could use to create the facet. Within the range facet the amount of  
milliseconds for the several, before, mentioned cases are definied:

"facets" : {  
"test" : {  
"range" : {  
"key\_script" : "doc['adddate'].date.getMillis - System.currentTimeInMillis",  
"value\_script" : "1",  
"ranges" : [  
{ "from" : "0", "to" : "604800000" },  
{ "from" : "604800001", "to" : "2678400000" },  
{ "from" : "2678400001", "to" : "2678400000" },  
{ "from" : "2678400001", "to" : "7776000000" },  
{ "from" : "7776000001", "to" : "31536000000" },  
{ "from" : "31536000001" }  
]  
}  
}  
}

However..... it fails with the script:

PropertyAccessException[[Error: could not access: getMillis; in class:  
org.elasticsearch.common.joda.time.MutableDateTime] [Near : {...  
doc['adddate'].date.getMillis ....}]

Are not all methods and properties of the Joda MutableDateTime exposed and  
if not..... is there another solution to get the above result. Is I only  
try the System.currentTimeInMillis, it fails as well.

Thanks in advance

BTW I'm using ES 0.90.0 Beta

--  
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:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [April 10, 2013, 3:49pm UTC](https://discuss.elastic.co/t/range-facet-with-dates-computing/11494/2 "2013-04-10T15:49:10Z")

</div>

This should work:

"facets" : {  
"test" : {  
"range" : {  
"key\_script" : "System.currentTimeMillis() -  
doc["adddate"].value",  
"value\_script" : "1",  
"ranges" : [  
{ "from" : 0, "to" : 604800000 },  
{ "from" : 604800001, "to" : 2678400000 },  
{ "from" : 2678400001, "to" : 7776000000 },  
{ "from" : 7776000001, "to" : 31536000000 },  
{ "from" : 31536000001 }  
]  
}  
}  
}

but you will might get faster performance if you will recalculate the  
ranges and use adddate as a key field instead of using key\_script.

On Monday, April 8, 2013 7:07:42 PM UTC-4, Erwin Rijss wrote:

> Hi All,
> 
> I like to create a facet list like:
> 
> - this week
> - last month
> - last quarter
> - last year
> - older
> 
> I have a dataset with an adddate field. The adddate in milliseconds minus  
> the current date/time in millisecons gives me an amount of milliseconds  
> which I could use to create the facet. Within the range facet the amount of  
> milliseconds for the several, before, mentioned cases are definied:
> 
> "facets" : {  
> "test" : {  
> "range" : {  
> "key\_script" : "doc['adddate'].date.getMillis -  
> System.currentTimeInMillis",  
> "value\_script" : "1",  
> "ranges" : [  
> { "from" : "0", "to" : "604800000" },  
> { "from" : "604800001", "to" : "2678400000" },  
> { "from" : "2678400001", "to" : "2678400000" },  
> { "from" : "2678400001", "to" : "7776000000" },  
> { "from" : "7776000001", "to" : "31536000000" },  
> { "from" : "31536000001" }  
> ]  
> }  
> }  
> }
> 
> However..... it fails with the script:
> 
> PropertyAccessException[[Error: could not access: getMillis; in class:  
> org.elasticsearch.common.joda.time.MutableDateTime] [Near : {...  
> doc['adddate'].date.getMillis ....}]
> 
> Are not all methods and properties of the Joda MutableDateTime exposed and  
> if not..... is there another solution to get the above result. Is I only  
> try the System.currentTimeInMillis, it fails as well.
> 
> Thanks in advance
> 
> BTW I'm using ES 0.90.0 Beta

--  
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:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 2:41am UTC](https://discuss.elastic.co/t/range-facet-with-dates-computing/11494/3 "2017-07-06T02:41:44Z")

</div>


