# Filtering documents by score via scripting

**URL:** https://discuss.elastic.co/t/filtering-documents-by-score-via-scripting/21141
**Category:** Elasticsearch
**Created:** [December 8, 2014, 11:55am UTC](https://discuss.elastic.co/t/filtering-documents-by-score-via-scripting/21141 "2014-12-08T11:55:14Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Pedro\_Cunha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pedro_cunha/32/1056_2.png) [@Pedro\_Cunha](https://discuss.elastic.co/u/Pedro_Cunha)
#### Post date: [December 8, 2014, 11:55am UTC](https://discuss.elastic.co/t/filtering-documents-by-score-via-scripting/21141/1 "2014-12-08T11:55:14Z")

</div>

Hello,

I have a specific scenario where I have an entity A that has collection of  
nested entities of B.

While searching on A index, I need to compute B's that match a certain  
criteria and then use scripting to calculate a specific value with specific  
params. This value is obtained from some computed information of N elements  
of B (sometimes just one B but it's not the rule). Ultimately this value  
might (and not necessarily) need to be filtered and/or sorted by.

Initially I started using filter script, loading data through \_source, and  
instantly I realised that when the dataset is around 10k documents (which  
from what I've seen in this forum is very small) the performance degrades  
from 300ms (dataset filtered to 2-3k documents) to 2seconds in one single  
request. A major problem also if the cluster receives a lot of these slow  
queries, it will degrade the overall response time of everything. (I know  
at this point RAM is enough and I'm already using a SSD).

I started looking for options and realised function\_score could be a good  
candidate to avoid the nasty \_source. I managed to come up with a way to  
calculate the value I need - still not very fast though - 800ms for the  
worst case scenario ( maybe can live with this, if 95% of my other queries  
are \< 100ms).

1. Should I be using function\_score for something that I need filtering  
or sorting ?
2. Regarding filtering the score ES allows a min\_score, but not a  
max\_score. The following query fails with this  
error: GroovyScriptExecutionException[GroovyRuntimeException[Cannot compare  
org.elasticsearch.script.ScoreAccessor with value  
'org.elasticsearch.script.ScoreAccessor@7c792ee' and java.lang.Integer with  
value '300'. Looking at the source code, do I need to call doubleValue() ?  
The query does start to work but I wonder why this is not in the  
documentation ?

{  
"min\_score": 0,  
"query": {  
"function\_score": {  
"boost\_mode": "multiply",  
"query": {  
"nested": {  
"path": "nested\_path",  
"query": {  
"function\_score" : {  
"score\_mode": "sum",  
"script\_score": {  
"script": "...",  
"params": {  
...  
}  
}  
}  
}  
}  
},  
"script\_score" : {  
"params" : { "min": 300, "max": 500 },  
"script" : "(\_score \> min) && (\_score \< max) ? 1 : -1"  
}  
}  
}  
}

Thanks in advance for any tips in this scenario, I would greatly appreciate  
any help & suggestions,  
Pedro

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/aa1036c2-05e8-4ba6-b493-0a90951e5058%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/aa1036c2-05e8-4ba6-b493-0a90951e5058%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:45am UTC](https://discuss.elastic.co/t/filtering-documents-by-score-via-scripting/21141/2 "2017-07-06T00:45:01Z")

</div>


