# Thoughts on replacing a date numeric range filter with a script filter

**URL:** <https://discuss.elastic.co/t/thoughts-on-replacing-a-date-numeric-range-filter-with-a-script-filter/11981>\
**Category:** Elasticsearch\
**Created:** [May 15, 2013, 8:59pm UTC](https://discuss.elastic.co/t/thoughts-on-replacing-a-date-numeric-range-filter-with-a-script-filter/11981 "2013-05-15T20:59:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_Waters](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tim_waters/32/2343_2.png) [@Tim\_Waters](https://discuss.elastic.co/u/Tim_Waters)\
**Post date:** [May 15, 2013, 8:59pm UTC](https://discuss.elastic.co/t/thoughts-on-replacing-a-date-numeric-range-filter-with-a-script-filter/11981/1 "2013-05-15T20:59:41Z")

</div>

Hi folks,

I want to be able to do some scripting in a numeric range of dates within a  
filtered query. So I though I would show you what I have done and ask you  
for your advice as I'm left with the suspicion that I could have done  
things differently or more elegantly!

I'm starting with something simple like this numeric\_range filter

{  
"numeric\_range" : {  
"start\_date" : {  
"gte" : "2010-01-01",  
"lte" : "2013-01-01"  
}  
}  
}

And going to something like this:

{  
"script": {  
"script": "doc['start\_date'].value / 1000 \>= start\_param && doc['  
start\_date'].value / 1000 \<= end\_param",  
"params": {  
"start\_param": 1262304000  
"end\_param": 1356998400  
}  
}  
}

The main differences are that the value of a date is in milliseconds, and I  
am passing in unix/epoch timestamps in seconds, hence the divide by 1000

Now, my actual use for this is for a script filter where "start\_range" also  
stored in the doc and represents the number of days:  
"script": "(doc['start\_date].value / 1000 )+ (doc['start\_range'].value \*  
86400) \<= param1 "

So, it works, and performs fine. My questions are: does this make sense?  
Could I have done it easier or cleaner? Is my solution worse for  
performance / caching than the numeric\_range? Have I missed something  
glaringly obvious? Perhaps mvel and elasticsearch expose some date values  
that I could use?

Cheers!

Tim

--  
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:** ![simonw\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simonw_2/32/1130_2.png) [@simonw\_2](https://discuss.elastic.co/u/simonw_2)\
**Post date:** [May 15, 2013, 9:27pm UTC](https://discuss.elastic.co/t/thoughts-on-replacing-a-date-numeric-range-filter-with-a-script-filter/11981/2 "2013-05-15T21:27:39Z")

</div>

hey,

On Wednesday, May 15, 2013 10:59:41 PM UTC+2, Tim Waters wrote:

> Hi folks,
> 
> I want to be able to do some scripting in a numeric range of dates within  
> a filtered query. So I though I would show you what I have done and ask  
> you for your advice as I'm left with the suspicion that I could have done  
> things differently or more elegantly!
> 
> I'm starting with something simple like this numeric\_range filter
> 
> {  
> "numeric\_range" : {  
> "start\_date" : {  
> "gte" : "2010-01-01",  
> "lte" : "2013-01-01"  
> }  
> }  
> }
> 
> And going to something like this:
> 
> {  
> "script": {  
> "script": "doc['start\_date'].value / 1000 \>= start\_param && doc['  
> start\_date'].value / 1000 \<= end\_param",  
> "params": {  
> "start\_param": 1262304000  
> "end\_param": 1356998400  
> }  
> }  
> }
> 
> The main differences are that the value of a date is in milliseconds, and  
> I am passing in unix/epoch timestamps in seconds, hence the divide by 1000
> 
> Now, my actual use for this is for a script filter where "start\_range"  
> also stored in the doc and represents the number of days:  
> "script": "(doc['start\_date].value / 1000 )+ (doc['start\_range'].value \*  
> 86400) \<= param1 "
> 
> So, it works, and performs fine. My questions are: does this make sense?  
> Could I have done it easier or cleaner? Is my solution worse for  
> performance / caching than the numeric\_range? Have I missed something  
> glaringly obvious? Perhaps mvel and elasticsearch expose some date values  
> that I could use?

the numeric range is actually executed on the inverted index and has very  
low memory requirements while is still fast. you can also index the  
timestamp in seconds if you want as a long and use numeric ranges on that  
so you get kind of 'best of both worlds'. The script is likely to be way  
slower if you match lots of docs and it needs to represent all dates in  
memory so that is 64bit per document heapspace.

hope that helps....

> Cheers!
> 
> Tim

--  
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:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [May 16, 2013, 9:10am UTC](https://discuss.elastic.co/t/thoughts-on-replacing-a-date-numeric-range-filter-with-a-script-filter/11981/3 "2013-05-16T09:10:36Z")

</div>

Heya Simon

As I understand it, the numeric\_range query uses fielddata, not the  
inverted index. The range query uses the inverted index.

clint

On 15 May 2013 23:27, simonw [simon.willnauer@elasticsearch.com](mailto:simon.willnauer@elasticsearch.com) wrote:

> hey,
> 
> On Wednesday, May 15, 2013 10:59:41 PM UTC+2, Tim Waters wrote:
> 
> > Hi folks,
> > 
> > I want to be able to do some scripting in a numeric range of dates within  
> > a filtered query. So I though I would show you what I have done and ask  
> > you for your advice as I'm left with the suspicion that I could have  
> > done things differently or more elegantly!
> > 
> > I'm starting with something simple like this numeric\_range filter
> > 
> > {  
> > "numeric\_range" : {  
> > "start\_date" : {  
> > "gte" : "2010-01-01",  
> > "lte" : "2013-01-01"  
> > }  
> > }  
> > }
> > 
> > And going to something like this:
> > 
> > {  
> > "script": {  
> > "script": "doc['start\_date'].value / 1000 \>= start\_param && doc['  
> > start\_date'].value / 1000 \<= end\_param",  
> > "params": {  
> > "start\_param": 1262304000  
> > "end\_param": 1356998400  
> > }  
> > }  
> > }
> > 
> > The main differences are that the value of a date is in milliseconds, and  
> > I am passing in unix/epoch timestamps in seconds, hence the divide by 1000
> > 
> > Now, my actual use for this is for a script filter where "start\_range"  
> > also stored in the doc and represents the number of days:  
> > "script": "(doc['start\_date].value / 1000 )+ (doc['start\_range'].value \*  
> > 86400) \<= param1 "
> > 
> > So, it works, and performs fine. My questions are: does this make sense?  
> > Could I have done it easier or cleaner? Is my solution worse for  
> > performance / caching than the numeric\_range? Have I missed something  
> > glaringly obvious? Perhaps mvel and elasticsearch expose some date values  
> > that I could use?
> 
> the numeric range is actually executed on the inverted index and has very  
> low memory requirements while is still fast. you can also index the  
> timestamp in seconds if you want as a long and use numeric ranges on that  
> so you get kind of 'best of both worlds'. The script is likely to be way  
> slower if you match lots of docs and it needs to represent all dates in  
> memory so that is 64bit per document heapspace.
> 
> hope that helps....
> 
> > Cheers!
> > 
> > Tim
> > 
> > --  
> > 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).

--  
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:36am UTC](https://discuss.elastic.co/t/thoughts-on-replacing-a-date-numeric-range-filter-with-a-script-filter/11981/4 "2017-07-06T02:36:23Z")

</div>


