# ES Script reusing the values

**URL:** <https://discuss.elastic.co/t/es-script-reusing-the-values/105451>\
**Category:** Elasticsearch\
**Created:** [October 26, 2017, 4:18pm UTC](https://discuss.elastic.co/t/es-script-reusing-the-values/105451 "2017-10-26T16:18:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![samatha\_kankipati](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@samatha\_kankipati](https://discuss.elastic.co/u/samatha_kankipati)\
**Post date:** [October 26, 2017, 4:18pm UTC](https://discuss.elastic.co/t/es-script-reusing-the-values/105451/1 "2017-10-26T16:18:49Z")

</div>

Hi,

We have script field stored in file. We need script field to display and also for sorting.  
Looks like script is executed two times, one for showing the field in the response and another time for sorting.

Is there any way, we can avoid this execution two times?  
i.e run the script one time, use this result for display and sort.

Thanks  
Samatha

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [November 7, 2017, 7:36pm UTC](https://discuss.elastic.co/t/es-script-reusing-the-values/105451/2 "2017-11-07T19:36:15Z")

</div>

The script is executed two times because it is used within two different contexts within the search. Instead of specifying a sort script, you can use a script score. The score is always returned, so you can use it for display, and it is also the default sort.

---

<div class="post-metadata">

**Author:** ![samatha\_kankipati](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@samatha\_kankipati](https://discuss.elastic.co/u/samatha_kankipati)\
**Post date:** [November 8, 2017, 4:28am UTC](https://discuss.elastic.co/t/es-script-reusing-the-values/105451/3 "2017-11-08T04:28:56Z")

</div>

@rjernst thanks for the reply..  
We are also planning to use that approach..

Only problem is, We have multiple script fields,  
Depending on request, we may have sort on one or more script fields  
So, checking if there is way to cache the results of script and use it in other place

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [November 10, 2017, 12:24am UTC](https://discuss.elastic.co/t/es-script-reusing-the-values/105451/4 "2017-11-10T00:24:29Z")

</div>

There is no way to cache results of scripts. These are executed in completely different ares of the codebase.

---

<div class="post-metadata">

**Author:** ![samatha\_kankipati](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@samatha\_kankipati](https://discuss.elastic.co/u/samatha_kankipati)\
**Post date:** [November 14, 2017, 3:24pm UTC](https://discuss.elastic.co/t/es-script-reusing-the-values/105451/5 "2017-11-14T15:24:09Z")

</div>

Thank you @rjernst

---

<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:** [December 12, 2017, 3:24pm UTC](https://discuss.elastic.co/t/es-script-reusing-the-values/105451/6 "2017-12-12T15:24:11Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
