# Same Script filter is Extremly fast and Extremly slow with a small change. Why

**URL:** <https://discuss.elastic.co/t/same-script-filter-is-extremly-fast-and-extremly-slow-with-a-small-change-why/8617>\
**Category:** Elasticsearch\
**Created:** [August 2, 2012, 11:37pm UTC](https://discuss.elastic.co/t/same-script-filter-is-extremly-fast-and-extremly-slow-with-a-small-change-why/8617 "2012-08-02T23:37:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pradeep\_2](https://avatars.discourse-cdn.com/v4/letter/p/9de053/32.png) [@Pradeep\_2](https://discuss.elastic.co/u/Pradeep_2)\
**Post date:** [August 2, 2012, 11:37pm UTC](https://discuss.elastic.co/t/same-script-filter-is-extremly-fast-and-extremly-slow-with-a-small-change-why/8617/1 "2012-08-02T23:37:11Z")

</div>

My script when used in filter, returns extremly slowly for the results I  
want, however, if I just make the script return any doc that doesn't match  
my criteria.. i,e. just by FLIPPING the RETURN statement , query returns  
quite fast.

same behavior is noticed every single time , even when .. out of 6k docs I  
query on, the fast case matches 5.5k docs and the slow case matches, well  
,the rest.. i.e. 0.5k  
Same behavior is noticed no matter how I change my input parameters... how  
I manipulate the script (without changing the bussiness logic)  
.\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_.

I am running on single node.. with Shards = 1, Replicas =0  
Example document looks like this..

{  
"data": {  
"id": "123",  
"timeseries": [{  
date: "2004-08-16T00:00:00",  
value: 3 },  
{  
date: "2004-12-14T00:00:00",  
value: 2  
}]  
}  
}

# i.e. each document has an array "timeseries" which stores historical data. I have a mvel script just comparing historical data..

as\_of\_val=0;  
foreach(val:\_source.data.timeseries)  
{  
if(val.date \< start\_date)  
{  
as\_of\_val = val.value;  
}  
}

ret = false;  
if( as\_of\_val == rating1 ){  
ret=true;  
}

return ret;

============================

Now above script is the logic I require... and it takes like 80 sec to  
return the filtered docs.. ( I only fetch 200 docs tho).. no matter how I  
change my input parameters.. (consequently regardless of number of docs  
matched)..

Now.. if I just change return statement to "return !ret" ... the query  
returns in 1 sec! no matter how I change my input parameters..  
(consequently regardless of number of docs matched)..

Not just that...even if I switch .. " ret = false; ret = true;"  
statements.. are whatever... when the script essentially does what my above  
script does.. it takes constantly 80sec... but when it essentially does  
the exact opposite.. it takes 1sec..

I tried putting my script in "not:{} " ... even then.. its same behavior...

Can anyone explain this behavior pls?

---

<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, 3:17am UTC](https://discuss.elastic.co/t/same-script-filter-is-extremly-fast-and-extremly-slow-with-a-small-change-why/8617/2 "2017-07-06T03:17:56Z")

</div>


