# Performance issue with script scoring with fields having a large array

**URL:** <https://discuss.elastic.co/t/performance-issue-with-script-scoring-with-fields-having-a-large-array/17275>\
**Category:** Elasticsearch\
**Created:** [April 30, 2014, 9:27am UTC](https://discuss.elastic.co/t/performance-issue-with-script-scoring-with-fields-having-a-large-array/17275 "2014-04-30T09:27:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicom/32/488_2.png) [@nicom](https://discuss.elastic.co/u/nicom)\
**Post date:** [April 30, 2014, 9:27am UTC](https://discuss.elastic.co/t/performance-issue-with-script-scoring-with-fields-having-a-large-array/17275/1 "2014-04-30T09:27:15Z")

</div>

I have document having fields containing large array.

I would like to score according to the value of a nth element of such  
array, but got very slow answer (5s) for only 10K document indexed.

my mapping:  
document {  
id: value,  
field2: string,  
field3: [int\_1,int\_2, ... , int\_10k] \<- large array of 10K integers  
}

assume I generated and indexed 10K documents with 1K random integer values  
in the field 'field3'

I then use the following search query

GET /test/document/\_search  
{  
"query":{  
"function\_score":{  
"script\_score" : {  
"script" : " \_source.fields3[12] \* \_source.fields3[11] "  
}

=\> got 5000 ms

however with basic Java object with a simple nested loop:

- for all the documents  
score[i] = doc[i].fields[12] \* doc[i].fields[11]
- sort by score

=\> got \< 50 ms

ES is 100 slower than a simple loop..

How to get similar performance with ES?

--  
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/db53da70-4f75-4088-b9a6-2cde3caef062%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/db53da70-4f75-4088-b9a6-2cde3caef062%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [April 30, 2014, 10:00am UTC](https://discuss.elastic.co/t/performance-issue-with-script-scoring-with-fields-having-a-large-array/17275/2 "2014-04-30T10:00:08Z")

</div>

Hello,

Using \_source for scripts is typically slow, because ES has to go to each  
stored document and extract fields from there. A faster approach is to use  
something like doc['field3'].values[12], which will used the field data  
cache (already loaded in memory, at least after the first run):

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

More details about field data can be found here:

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

## Best regards, Radu

Performance Monitoring \* Log Analytics \* Search Analytics  
Solr & Elasticsearch Support \* [http://sematext.com/](http://sematext.com/)

On Wed, Apr 30, 2014 at 12:27 PM, NM [n.maisonneuve@gmail.com](mailto:n.maisonneuve@gmail.com) wrote:

> I have document having fields containing large array.
> 
> I would like to score according to the value of a nth element of such  
> array, but got very slow answer (5s) for only 10K document indexed.
> 
> my mapping:  
> document {  
> id: value,  
> field2: string,  
> field3: [int\_1,int\_2, ... , int\_10k] \<- large array of 10K integers  
> }
> 
> assume I generated and indexed 10K documents with 1K random integer values  
> in the field 'field3'
> 
> I then use the following search query
> 
> GET /test/document/\_search  
> {  
> "query":{  
> "function\_score":{  
> "script\_score" : {  
> "script" : " \_source.fields3[12] \* \_source.fields3[11] "  
> }
> 
> =\> got 5000 ms
> 
> however with basic Java object with a simple nested loop:
> 
> - for all the documents  
> score[i] = doc[i].fields[12] \* doc[i].fields[11]
> - sort by score
> 
> =\> got \< 50 ms
> 
> ES is 100 slower than a simple loop..
> 
> How to get similar performance with ES?
> 
> --  
> 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/db53da70-4f75-4088-b9a6-2cde3caef062%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/db53da70-4f75-4088-b9a6-2cde3caef062%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/db53da70-4f75-4088-b9a6-2cde3caef062%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/db53da70-4f75-4088-b9a6-2cde3caef062%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/CAHXA0\_2wmDJFBJvJ1fTUsszaP7GjVtJYfSU-AbHMq6NS%2BVqhFw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHXA0_2wmDJFBJvJ1fTUsszaP7GjVtJYfSU-AbHMq6NS%2BVqhFw%40mail.gmail.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, 1:32am UTC](https://discuss.elastic.co/t/performance-issue-with-script-scoring-with-fields-having-a-large-array/17275/3 "2017-07-06T01:32:31Z")

</div>


