Script query started to fail suddenly

We have had this query for a long time

{
  "size": 50,
  "query": {
   "match_all": {}
  },
  "version": false,
  "seq_no_primary_term": false,
  
  "sort": [
    {
      "_script": {
        "script": {
          "source": "(doc[params.f].size() != 0) ? (params.m['' + doc[params.f].value] ?: params.o): params.o",
          "lang": "painless",
          "params": {
            "f": "scoreSerial",
            "m": {
              "0": "UNDEFINED",
              "1": "FRUSTRATED",
              "2": "TOLERATED",
              "3": "SATISFIED"
            },
            "o": "ZZZZZ"
          }
        },
        "type": "string",
        "order": "asc"
      }
    }
  ]
} 

This suddenly stopped working with the error

{
  "error" : {
    "root_cause" : [
      {
        "type" : "bootstrap_method_error",
        "reason" : "bootstrap_method_error: CallSite bootstrap method initialization exception"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "visit-global-standard-w2022.31",
        "node" : "olbLlZh7RYuwaf5S-B9v8g",
        "reason" : {
          "type" : "script_exception",
          "reason" : "runtime error",
          "script_stack" : [
            "(doc[params.f].size() != 0) ? (params.m['' + doc[params.f].value] ?: params.o): params.o",
            "                                                          ^---- HERE"
          ],
          "script" : "(doc[params.f].size() != 0) ? (params.m['' + doc[params.f].value] ?: params.o): params.o",
          "lang" : "painless",
          "position" : {
            "offset" : 58,
            "start" : 0,
            "end" : 88
          },
          "caused_by" : {
            "type" : "bootstrap_method_error",
            "reason" : "bootstrap_method_error: CallSite bootstrap method initialization exception"
          }
        }
      }
    ],
    "caused_by" : {
      "type" : "bootstrap_method_error",
      "reason" : "bootstrap_method_error: CallSite bootstrap method initialization exception"
    }
  },
  "status" : 400
}

It was working for more than 3 years. We are now on 7.10 (for more than 6 months, when the above script was working) ES version. But suddenly it started to fail.
The strange thing is that it is not happening in all environments. Is it some java version related or something else? We can not also reproduce this locally.