Script exception runtime error

Hi there,

i have created scripted field for one of my index and here is the script:

def path = doc['request_id.keyword'].value;

if (path != null) {

    def first = path.indexOf("_");

    def second = first + 1 + path.substring(first + 1).indexOf("_");

    def last = path.lastIndexOf("_");

    return path.substring(first + 1, second);

}
return "";

but when i go to discover and load data for last 2 hours, i receive some error. Here is the detail:

{
  "took" : 4225,
  "timed_out" : false,
  "_shards" : {
    "total" : 140,
    "successful" : 139,
    "skipped" : 120,
    "failed" : 1,
    "failures" : [
      {
        "shard" : 2,
        "index" : "mashery-logs.2022.05.26",
        "node" : "wWo9DjrGQJaqhX2ybntIxQ",
        "reason" : {
          "type" : "script_exception",
          "reason" : "runtime error",
          "script_stack" : [
            "org.elasticsearch.index.fielddata.ScriptDocValues$Strings.get(ScriptDocValues.java:568)",
            "org.elasticsearch.index.fielddata.ScriptDocValues$Strings.getValue(ScriptDocValues.java:584)",
            """path = doc['request_id.keyword'].value;

""",
            "                                ^---- HERE"
          ],
          "script" : "def path = doc['request_id.keyword'].value; ...",
          "lang" : "painless",
          "position" : {
            "offset" : 36,
            "start" : 4,
            "end" : 47
          },
          "caused_by" : {
            "type" : "illegal_state_exception",
            "reason" : "A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!"
          }
        }
      }
    ]
  },

can you help me to solve this problem?

The "reason" property tells you what's happening:

You need to check whether a field exists on a document before accessing it.

actually this is a .keyword field and the script aims to solve it. Here is the sample of the request_id value



the situation is same as you say. my request_id is indexed as text field and that's why i created request_id.keyword field in my mappings

The error is not about keyword vs text - you have at least one document in your index which doesn't have a value for this field at all. Either make sure every single document has a value for that field or add the safeguard for only accessing the field if it's set on the current document