# Script exception runtime error

**URL:** https://discuss.elastic.co/t/script-exception-runtime-error/305689
**Category:** Kibana
**Tags:** painless
**Created:** [May 26, 2022, 9:00am UTC](https://discuss.elastic.co/t/script-exception-runtime-error/305689 "2022-05-26T09:00:36Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![yuswanul](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuswanul/32/101662_2.png) [@yuswanul](https://discuss.elastic.co/u/yuswanul)
#### Post date: [May 26, 2022, 9:00am UTC](https://discuss.elastic.co/t/script-exception-runtime-error/305689/1 "2022-05-26T09:00:36Z")

</div>

Hi there,

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

```auto
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:

```auto
{
  "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?

---

<div class="post-metadata">

### Author: ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)
#### Post date: [May 26, 2022, 3:41pm UTC](https://discuss.elastic.co/t/script-exception-runtime-error/305689/2 "2022-05-26T15:41:12Z")

</div>

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

> [@yuswanul](#):
>
> `A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!`

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

---

<div class="post-metadata">

### Author: ![yuswanul](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuswanul/32/101662_2.png) [@yuswanul](https://discuss.elastic.co/u/yuswanul)
#### Post date: [May 26, 2022, 11:13pm UTC](https://discuss.elastic.co/t/script-exception-runtime-error/305689/3 "2022-05-26T23:13:38Z")

</div>

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

 ![WhatsApp Image 2022-05-26 at 15.19.06](https://us1.discourse-cdn.com/elastic/original/3X/0/3/03636b5009026d45cb014e4b2b2afd9c9e666b2c.jpeg)  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/b/2b938c6401958aa098cdce09df98f85a2ff0531d.png)  
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

---

<div class="post-metadata">

### Author: ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)
#### Post date: [May 30, 2022, 3:07pm UTC](https://discuss.elastic.co/t/script-exception-runtime-error/305689/4 "2022-05-30T15:07:19Z")

</div>

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

---

<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 27, 2022, 3:08pm UTC](https://discuss.elastic.co/t/script-exception-runtime-error/305689/5 "2022-06-27T15:08:02Z")

</div>

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