Search code examples
elasticsearchelasticsearch-painlesselasticsearch-7

Elasticsearch 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
}

The mapping of scoreSerial is

 "scoreSerial" : {
        "type" : "long"
 }

It was working for more than 3 years. We are now on the 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.


Solution

  • It might have to do with type coercion, I've seen similar problems in the past (although not with painless but groovy), so simply replacing

    '' + doc[params.f].value
    

    by

    doc[params.f].value.toString()
    

    might do the trick

    (i.e. explicitly getting the string version of the doc value instead of relying on type coercion). What you did should work according to the docs, though.