# Elastic Scripting caching issue

**URL:** <https://discuss.elastic.co/t/elastic-scripting-caching-issue/273511>\
**Category:** Elasticsearch\
**Created:** [May 20, 2021, 10:59am UTC](https://discuss.elastic.co/t/elastic-scripting-caching-issue/273511 "2021-05-20T10:59:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Divy\_Garg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/divy_garg/32/79288_2.png) [@Divy\_Garg](https://discuss.elastic.co/u/Divy_Garg)\
**Post date:** [May 20, 2021, 10:59am UTC](https://discuss.elastic.co/t/elastic-scripting-caching-issue/273511/1 "2021-05-20T10:59:22Z")

</div>

I am facing caching issue while using scripts.  
So I am using below script in elasticsearch query.  
But when it runs multiple times it gives circuit breaker exception( **Too many dynamic script compilations within, max: [75/5m]**) as my **key** keeps changing. So it caches everytime a new key is there. I tried to look for params help but I couldn't find any help where doc key is passed as param value. Can you help how can we achieve this?

"(doc.containsKey( **key** ) && (doc[**key**] != null ||doc[**key**] == 0)) ? doc[**key**] : 1

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 20, 2021, 11:49am UTC](https://discuss.elastic.co/t/elastic-scripting-caching-issue/273511/2 "2021-05-20T11:49:57Z")

</div>

In which context are you using this script?

---

<div class="post-metadata">

**Author:** ![Divy\_Garg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/divy_garg/32/79288_2.png) [@Divy\_Garg](https://discuss.elastic.co/u/Divy_Garg)\
**Post date:** [May 20, 2021, 11:55am UTC](https://discuss.elastic.co/t/elastic-scripting-caching-issue/273511/3 "2021-05-20T11:55:41Z")

</div>

Hi @dadoonet Can you please elaborate more on context which you are asking. Not aware of context here.  
I am using default painless script in elastic cluster 6.4.2 if that is what you are looking for.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 20, 2021, 2:37pm UTC](https://discuss.elastic.co/t/elastic-scripting-caching-issue/273511/4 "2021-05-20T14:37:26Z")

</div>

What is the exact and full request you are sending? Which API?

---

<div class="post-metadata">

**Author:** ![Divy\_Garg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/divy_garg/32/79288_2.png) [@Divy\_Garg](https://discuss.elastic.co/u/Divy_Garg)\
**Post date:** [May 20, 2021, 7:22pm UTC](https://discuss.elastic.co/t/elastic-scripting-caching-issue/273511/5 "2021-05-20T19:22:07Z")

</div>

HI @dadoonet ,

I am sending the \_search request on one of the index with body as :

```auto
{
  "from": 0,
  "size": 20,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "is_in_stock": 1
          }
        }
      ]
    }
  },
  "aggs": {
    "min_price": {
      "aggs": {
        "min_price": {
          "nested": {
            "path": "store"
          },
          "aggs": {
            "min_price": {
              "min": {
                "script": "(doc.containsKey('store.warehouse_1162652_price') && (doc['store.warehouse_1162652_price'] != null || doc['store.warehouse_1162652_price'] == 0)) ? doc['store.warehouse_1162652_price'] : doc['store.price']"
              }
            }
          }
        }
      }
    }
  }
}

```

Here **store.warehouse\_1162652\_price** varies with every request. Like it is store.warehouse\_1162652\_price for this request. It can be store.warehouse\_1162687\_price for another request .So I am trying to use params for the script. Need to understand how can i write that.

---

<div class="post-metadata">

**Author:** ![mbrad](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@mbrad](https://discuss.elastic.co/u/mbrad)\
**Post date:** [May 20, 2021, 7:33pm UTC](https://discuss.elastic.co/t/elastic-scripting-caching-issue/273511/6 "2021-05-20T19:33:41Z")

</div>

I am also having this issue with scripted metric aggregations with stored scripts. The process taken to understand the issue is:

1. a script code is updated and then pushed to the \_scripts/ api overwriting the current stored script file.
2. the scripted metric that references the stored script will about 50% of the time run an older version of the script or it will run the new script. Spamming the play button will show differences in responses about 50% of the time.

It feels like a caching issue of the old script. I can provide more details if needed.

```
GET index/_search
{
"aggs": {
"count_of_open": {
  "scripted_metric": {
    "init_script": {"id": "open_closed_init-map-test"},
    "map_script": {"id": "open_closed_map-script-map"},
    "combine_script":{"id": "open_closed_combine-script-map"},
    "reduce_script": {"id": "open_closed_reduction-map"}
  },
  "aggs": {
    "cloudType": {
      "terms": {
        "field": "cloudType.keyword",
        "size": 10
      }
    }
  }
}
},
"size": 0
}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 25, 2021, 1:29pm UTC](https://discuss.elastic.co/t/elastic-scripting-caching-issue/273511/7 "2021-05-25T13:29:10Z")

</div>

Could you write something like this:

```auto
{
  "from": 0,
  "size": 20,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "is_in_stock": 1
          }
        }
      ]
    }
  },
  "aggs": {
    "min_price": {
      "aggs": {
        "min_price": {
          "nested": {
            "path": "store"
          },
          "aggs": {
            "min_price": {
              "min": {
                "script": {
                  "source": "(doc.containsKey(params.field) && (doc[params.field] != null || doc[params.field] == 0)) ? doc[params.field] : doc['store.price']",
                  "params": {
                    "field": "store.warehouse_1162652_price"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Divy\_Garg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/divy_garg/32/79288_2.png) [@Divy\_Garg](https://discuss.elastic.co/u/Divy_Garg)\
**Post date:** [May 28, 2021, 6:30am UTC](https://discuss.elastic.co/t/elastic-scripting-caching-issue/273511/8 "2021-05-28T06:30:04Z")

</div>

Hi @dadoonet

This seems to be working. Thanks a lot for your help.  
Cheers!

---

<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:** [June 25, 2021, 6:31am UTC](https://discuss.elastic.co/t/elastic-scripting-caching-issue/273511/9 "2021-06-25T06:31:02Z")

</div>

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