# How many instances of script does ElasticSearch create when it calculates a score for document?

**URL:** <https://discuss.elastic.co/t/how-many-instances-of-script-does-elasticsearch-create-when-it-calculates-a-score-for-document/68174>\
**Category:** Elasticsearch\
**Created:** [December 6, 2016, 1:12pm UTC](https://discuss.elastic.co/t/how-many-instances-of-script-does-elasticsearch-create-when-it-calculates-a-score-for-document/68174 "2016-12-06T13:12:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![statut](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@statut](https://discuss.elastic.co/u/statut)\
**Post date:** [December 6, 2016, 1:12pm UTC](https://discuss.elastic.co/t/how-many-instances-of-script-does-elasticsearch-create-when-it-calculates-a-score-for-document/68174/1 "2016-12-06T13:12:24Z")

</div>

We use custom native script that is written in Java to calculate document's score in the ElasticSearch so we want to know how many instances of script the ElasticSearch creates when it calculates a score for document? One script per document, per shard?

Thanks.

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [December 6, 2016, 2:06pm UTC](https://discuss.elastic.co/t/how-many-instances-of-script-does-elasticsearch-create-when-it-calculates-a-score-for-document/68174/2 "2016-12-06T14:06:24Z")

</div>

Hi @statut,

search scripts get compiled once per node and are cached afterwards. You can control the script cache with `script.cache.expire` (expiry in ms) and `script.cache.max_size` (maximum number of cached scripts).

Daniel

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [December 6, 2016, 5:56pm UTC](https://discuss.elastic.co/t/how-many-instances-of-script-does-elasticsearch-create-when-it-calculates-a-score-for-document/68174/3 "2016-12-06T17:56:37Z")

</div>

Implementations of `ExecutableScript` and `SearchScript` are built fairly infrequently but not cached outside of the operations that they are performing. Like reindex will build the `ExecutableScript`once, when it first has to run the script and will reuse it forever. Script score functions build on `SearchScript` instance per shard.

It is instances of `CompiledScript` that are cached like @danielmitterdorfer says.

The lifecycle of a script isn't a thing we really guarantee though. So while I doubt we'l cache instances of `SearchScript` in the future because they are cheap to create you are still getting into an area that we might change as time goes on.

---

<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:** [January 3, 2017, 5:56pm UTC](https://discuss.elastic.co/t/how-many-instances-of-script-does-elasticsearch-create-when-it-calculates-a-score-for-document/68174/4 "2017-01-03T17:56:54Z")

</div>

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