# Elastic Update Script avoid dynamic script compilations

**URL:** <https://discuss.elastic.co/t/elastic-update-script-avoid-dynamic-script-compilations/295236>\
**Category:** Elasticsearch\
**Created:** [January 24, 2022, 2:03pm UTC](https://discuss.elastic.co/t/elastic-update-script-avoid-dynamic-script-compilations/295236 "2022-01-24T14:03:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Siegi](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@Siegi](https://discuss.elastic.co/u/Siegi)\
**Post date:** [January 24, 2022, 2:03pm UTC](https://discuss.elastic.co/t/elastic-update-script-avoid-dynamic-script-compilations/295236/1 "2022-01-24T14:03:14Z")

</div>

Hey there,  
after my elastic usage is growing, I ran into the script.max\_compilations\_rate(

TransportError(500, 'general\_script\_exception', '[script] Too many dynamic  
script compilations

) in my updates. I use update by query scrips ins my Python application, because I have not saved the document ids outside to use them for updates. An update looks sth like this:

```auto
{
            "script": {
                "source": "ctx._source.category_id = params.cat",
                "params": {"cat":2}
            },
            "query": {
                "term": {
                    "feed_id": 164794
                }
            }
        }

```

I guess I have 3 options here: (correct me if am wrong)

1. update max\_compilations\_rate: I don't wanna do that for performance reasons
2. Reindex all documents using feed\_id as document id and then use "Update API"
3. Change Update Script/method (?)

Is there a way to do this kind of updates(without knowing document id) with indexed scripts/without dynamic scripts?  
If yes, where can I find the right documentation?  
If no, how can I create a reindex pipeline which uses "feed\_id" as doc id(or is this even possibile)?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [January 25, 2022, 12:38pm UTC](https://discuss.elastic.co/t/elastic-update-script-avoid-dynamic-script-compilations/295236/2 "2022-01-25T12:38:33Z")

</div>

if that is your script beeing used across all updates, then it is unlikely that the too many dynamic script compilations stems from this script, but from others.

Is this the only script. you are using in your updates? Or just one of many?

---

<div class="post-metadata">

**Author:** ![Siegi](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@Siegi](https://discuss.elastic.co/u/Siegi)\
**Post date:** [January 25, 2022, 1:22pm UTC](https://discuss.elastic.co/t/elastic-update-script-avoid-dynamic-script-compilations/295236/3 "2022-01-25T13:22:48Z")

</div>

This script is used for updates of my Python Application using the update\_by\_query method of the Elasticsearch Python client. It is performed multiple 100 times per minute with different feed\_id 's and different or multiple parameters used for the updates.

I forgot to mention that the script in my question is just one example of the scripts which are used for updating.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [January 25, 2022, 4:06pm UTC](https://discuss.elastic.co/t/elastic-update-script-avoid-dynamic-script-compilations/295236/4 "2022-01-25T16:06:28Z")

</div>

The main question here is how many **different** scripts are in use?

---

<div class="post-metadata">

**Author:** ![Siegi](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@Siegi](https://discuss.elastic.co/u/Siegi)\
**Post date:** [January 26, 2022, 6:30pm UTC](https://discuss.elastic.co/t/elastic-update-script-avoid-dynamic-script-compilations/295236/5 "2022-01-26T18:30:41Z")

</div>

Its mainly the one above and two others(which are search scripts for calculating cosine similaries), but I just saw exceptions from the query above.

```auto
{
            "min_score": self._MIN_SCORE,
            "from": offset,
            "size": limit,
            "sort": [
                {"_score": {"order": sorting}}
            ],
            "query": {
                "script_score": {
                    "query": {
                        "bool": {
                            "filter": [
                                {"range": {
                                    "follower_count": {
                                        "gte": min_follower_count,
                                        "lte": max_follower_count
                                    }
                                }
                                }
                            ],
                            "must_not":
                                {
                                    "terms": {"feed_id": exclude}
                                }
                        }
                    },
                    "script": {
                        "source": "cosineSimilarity(params.tv, 'topicvector') + 1.0",
                        "params": {
                            "tv": query_vector,
                        }
                    }
                }
            }, "fields": ["feed_id"], "_source": False
        }

```

and

```auto
{
            "query": {
                "script_score": {
                    "query": {
                        "bool": {
                            "filter": [
                                {
                                    "terms": {
                                        "feed_id": query_feed_ids
                                    }
                                }
                            ]
                        }
                    },
                    "script": {
                        "source": "cosineSimilarity(params.tv, 'topicvector') + 1.0",
                        "params": {
                            "tv": target_vector
                        }
                    }
                }
            }, "fields": ["feed_id"], "_source": True
        }

```

I also tested my Application without any other scripts (by deactivating all other endpoints which perform other scripts)and the error still appeared.

Could it be that the script above has to be compiled each time it is executed because of different amount of parameters?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 17, 2022, 8:51am UTC](https://discuss.elastic.co/t/elastic-update-script-avoid-dynamic-script-compilations/295236/6 "2022-02-17T08:51:42Z")

</div>

You can increase logging for the component that compiles the script to see what is actually compiled. **Make sure you disable this again at some point to prevent lots of logging**

See [Logging | Elasticsearch Guide [7.17] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/logging.html#configuring-logging-levels)

The logger in question would be `org.elasticsearch.script.ScriptService`.

Hope this helps!

---

<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:** [March 17, 2022, 8:52am UTC](https://discuss.elastic.co/t/elastic-update-script-avoid-dynamic-script-compilations/295236/7 "2022-03-17T08:52:03Z")

</div>

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