# Speed up custom search query

**URL:** <https://discuss.elastic.co/t/speed-up-custom-search-query/12130>\
**Category:** Elasticsearch\
**Created:** [May 26, 2013, 8:20pm UTC](https://discuss.elastic.co/t/speed-up-custom-search-query/12130 "2013-05-26T20:20:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eugene\_Strokin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eugene_strokin/32/1238_2.png) [@Eugene\_Strokin](https://discuss.elastic.co/u/Eugene_Strokin)\
**Post date:** [May 26, 2013, 8:20pm UTC](https://discuss.elastic.co/t/speed-up-custom-search-query/12130/1 "2013-05-26T20:20:09Z")

</div>

Hello, in one of my application I need to match colors to find the closest  
one. Each document has several colors in Lab. I choose to write  
customScoreQuery and calculate score based on square root of SUM of deltas  
of the Lab numbers of the colors. Everything works fine, except the query  
is running very slow. I have millions of the documents, and it is  
ever-growing document base. So every day the query is slower and slower.  
What could be done to improve the performance? May be there is another way  
to make such searches, using something else besides Custom Score Queries?  
QueryBuilders.customScoreQuery(  
QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), fb))  
.script("res=0; " +  
"for(entry:\_source.colors){ " +  
"rt=sqrt(" +  
"pow(entry.colorL-l, 2)" +  
"+pow(entry.colorA-a, 2)" +  
"+pow(entry.colorB-b, 2)); " +  
"if(rt==0){rt=0.000001};"+  
"col=entry.area/rt;"+  
"if(col\>res){res=col};" +  
"} res")  
.param("l", lab[0])  
.param("a", lab[1])  
.param("b", lab[2])

I'd appreciate any ideas or comments.

Thank you,

Eugene

--  
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:34am UTC](https://discuss.elastic.co/t/speed-up-custom-search-query/12130/2 "2017-07-06T02:34:42Z")

</div>


