Elastic query scipting issue using painless ( Too many dynamic script compilations )

A functional script query is working fine when i dont add certain scripts to it but it fails and returns > [script] Too many dynamic script compilations within, max: [75/5m]; please use indexed, or scripts with parameters instead; this limit can be changed by the [script.max_compilations_rate] setting > this error when i add mainly 2 scripts to it. Also this is not happening everytime, sometimes it works fine but most of the times it returns error. Below are the queries :

Working query :

{
  "_source": false,
  "query": {
    "function_score": {
      "functions": [
        {
          "script_score": {
            "script": {
              "source": "doc['profile_completed_score'].value >0 ? doc['profile_completed_score'].value * 0.15 : 0"
            }
          }
        },
        {
          "script_score": {
            "script": {
              "source": "if(!doc['profile_pic'].empty)return 10; return 0;"
            }
          }
        },
        {
          "gauss": {
            "city_geolocation": {
              "origin": {
                "lat": "28.536000",
                "lon": "77.391000"
              },
              "scale": "100km",
              "offset": "100km",
              "decay": 0.0001
            }
          },
          "weight": 15
        },
        {
          "script_score": {
            "script": {
              "source": "def designation=0; if(doc['designation_name.keyword'].value != null && doc['designation_name.keyword'].value==params.designation) { designation=30; } else { if(doc['designation_name.keyword'].value != null && (doc['designation_name.keyword'].value.toUpperCase().indexOf(params.designation.toUpperCase())>-1 || params.designation.toUpperCase().indexOf(doc['designation_name.keyword'].value.toUpperCase())>-1)) designation=15; } return designation;",
              "params": {
                "designation": "Technical Manager"
              }
            }
          }
        },
        {
          "script_score": {
            "script": {
              "source": "def visited=0;def vscore=0;if(!doc['visited_event'].empty && doc['visited_event'].values.contains(params.fact))visited=10;if(!doc['avg_visitor_score'].empty)vscore=doc['avg_visitor_score'].value*0.2; return visited+vscore;",
              "params": {
                "fact": "13039"
              }
            }
          }
        }
      ],
      "score_mode": "sum",
      "boost_mode": "replace"
    }
  },
  "size": 20,
  "from": 0
}

Query returning error most of times :

{
  "_source": false,
  "query": {
    "function_score": {
      "functions": [
        {
          "script_score": {
            "script": {
              "source": "doc['profile_completed_score'].value >0 ? doc['profile_completed_score'].value * 0.15 : 0"
            }
          }
        },
        {
          "script_score": {
            "script": {
              "source": "if(!doc['profile_pic'].empty)return 10; return 0;"
            }
          }
        },
        {
          "gauss": {
            "city_geolocation": {
              "origin": {
                "lat": "28.536000",
                "lon": "77.391000"
              },
              "scale": "100km",
              "offset": "100km",
              "decay": 0.0001
            }
          },
          "weight": 15
        },
        {
          "script_score": {
            "script": {
              "source": "def designation=0; if(doc['designation_name.keyword'].value != null && doc['designation_name.keyword'].value==params.designation) { designation=30; } else { if(doc['designation_name.keyword'].value != null && (doc['designation_name.keyword'].value.toUpperCase().indexOf(params.designation.toUpperCase())>-1 || params.designation.toUpperCase().indexOf(doc['designation_name.keyword'].value.toUpperCase())>-1)) designation=15; } return designation;",
              "params": {
                "designation": "Technical Manager"
              }
            }
          }
        },
        {
          "script_score": {
           "script": {
              "source": "if(!doc['email_active'].empty && doc['email_active'].toInstant().toEpochMilli()/(params.divi) >= (params.epochtime)) return 30; return 0;",
              "params": {
                "divi": 1000,
                "epochtime": 1607863137
              }
            }
          }
        },
        {
          "script_score": {
            "script": {
              "source": "if((!doc['checkin_edition'].empty && doc['checkin_edition'].contains(params.fact))) return 30; return 0;",
              "params": {
                "fact": "1330689"
              }
            }
          }
        },
        {
          "script_score": {
            "script": {
              "source": "def visited=0;def vscore=0;if(!doc['visited_event'].empty && doc['visited_event'].values.contains(params.fact))visited=10;if(!doc['avg_visitor_score'].empty)vscore=doc['avg_visitor_score'].value*0.2; return visited+vscore;",
              "params": {
                "fact": "13039"
              }
            }
          }
        }
      ],
      "score_mode": "sum",
      "boost_mode": "replace"
    }
  },
  "size": 20,
  "from": 0
}

I tried indexing the scripts and using them with parameters but it didn't work.
Also tried increasing the limit to 150/5m but it is breaking at some point again

Any kind of help is much appriciated.

Thanks//

You should use parameters. What did the scripts look like when you tried using parameters?

I defined parameter for every value possible. It looked something like this :

"functions": [
        {
          "script_score": {
            "script": {
              "source": "doc['profile_completed_score'].value >(params.zero) ? doc['profile_completed_score'].value * (params.mul) : (params.zero)", 
              "params": {
                "mul" : 0.15,
                "zero" : 0
              }
            }
          }
        },
        {
          "script_score": {
            "script": {
              "source": "if(!doc['profile_pic'].empty)return (params.ten); return (params.zero);",
              "params": {
                "ten" : 10,
                "zero" : 0
              }
            }
          }
        },
        {
          "gauss": {
            "city_geolocation": {
              "origin": {
                "lat": "13.010000",
                "lon": "-61.231000"
              },
              "scale": "100km",
              "offset": "100km",
              "decay": 0.0001
            }
          },
          "weight": 15
        },
        {
          "script_score": {
            "script": {
              "source": "def designation=(params.zero); if(doc['designation_name.keyword'].value != (params.null) && doc['designation_name.keyword'].value==params.designation) { designation=(params.thirty); } else { if(doc['designation_name.keyword'].value != (params.null) && (doc['designation_name.keyword'].value.toUpperCase().indexOf(params.designation.toUpperCase())>(params.neg) || params.designation.toUpperCase().indexOf(doc['designation_name.keyword'].value.toUpperCase())>(params.neg))) designation=(params.fif); } return designation;",
              "params": {
                "designation": "Designer",
                "neg" : -1,
                "zero" : 0,
                "thirty" : 30,
                "fif" : 15,
                "null": null
              }
            }
          }
        },
        {
          "script_score": {
            "script": {
              "source": "if(!doc['email_active'].empty && doc['email_active'].value.getMillis()/(params.divi) >= (params.epochtime)) return (params.thirty); return (params.zero);",
              "params": {
                "epochtime" : 1606716947,
                "divi" : 1000,
                "thirty" : 30,
                "zero" : 0
              }
            }
          }
        },
        {
          "script_score": {
            "script": {
              "source": "if((!doc['checkin_edition'].empty && doc['checkin_edition'].values.contains(params.fact))) return (params.thirty); return (params.zero);",
              "params": {
                "fact": "718544",
                "zero" : 0,
                "thirty": 30
              }
            }
          }
        },
        {
          "script_score": {
            "script": {
              "source": "def visited=(params.zero);def vscore=(params.zero);if(!doc['visited_event'].empty && doc['visited_event'].values.contains(params.fact))visited=(params.ten);if(!doc['avg_visitor_score'].empty)vscore=doc['avg_visitor_score'].value*(params.mul); return visited+vscore;",
              "params": {
                "fact": "13039",
                "zero" : 0,
                "ten" : 10,
                "mul": 0.12
              }
            }
          }
        }
      ],

Have you looked at your script cache eviction stats?

If you see a lot of cache evictions, you may want to increase the script.cache.max_size setting because you may be thrashing your script compilation cache.

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