# \[script\_exception\] runtime error in Discover

**URL:** <https://discuss.elastic.co/t/script-exception-runtime-error-in-discover/252218>\
**Category:** Kibana\
**Created:** [October 15, 2020, 2:40pm UTC](https://discuss.elastic.co/t/script-exception-runtime-error-in-discover/252218 "2020-10-15T14:40:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mats.ahlbom](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@mats.ahlbom](https://discuss.elastic.co/u/mats.ahlbom)\
**Post date:** [October 15, 2020, 2:40pm UTC](https://discuss.elastic.co/t/script-exception-runtime-error-in-discover/252218/1 "2020-10-15T14:40:15Z")

</div>

Hi,

I can add scripted field selfReview;

```auto
if ( doc['type.keyword'].value == "comment-added" &&
     doc['comment.keyword'].value.contains("Code-Review+2") &&
     doc['change.owner.name.keyword'].value == doc['author.name.keyword'].value ) {
    return true;
}
return false;

```

The script runs fine when I run the test.  
But in Discover I get [script\_exception] runtime error. If I remove the above it´s works fine.

Full error message:

```auto
Error: Bad Request
    at Fetch._callee3$ (https://elk-kibana01/31997/bundles/commons.bundle.js:3:1287438)
    at l (https://elk-kibana01/31997/bundles/kbn-ui-shared-deps/kbn-ui-shared-deps.js:288:969217)
    at Generator._invoke (https://elk-kibana01/31997/bundles/kbn-ui-shared-deps/kbn-ui-shared-deps.js:288:968970)
    at Generator.forEach.e.<computed> [as next] (https://elk-kibana01/31997/bundles/kbn-ui-shared-deps/kbn-ui-shared-deps.js:288:969574)
    at asyncGeneratorStep (https://elk-kibana01/31997/bundles/commons.bundle.js:3:1280961)
    at _next (https://elk-kibana01/31997/bundles/commons.bundle.js:3:1281272)

```

---

<div class="post-metadata">

**Author:** ![thomasneirynck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomasneirynck/32/23313_2.png) [@thomasneirynck](https://discuss.elastic.co/u/thomasneirynck)\
**Post date:** [October 15, 2020, 9:19pm UTC](https://discuss.elastic.co/t/script-exception-runtime-error-in-discover/252218/2 "2020-10-15T21:19:31Z")

</div>

hi @mats.ahlbom,

this can happen when the script fails on a particular document. e.g. when your document is sparsely populated.

You can test this by running the `_search` request with the script-field directly against Elasticsearch.

You can grab the request discover is running by clicking on the `inspect`-tab in the top right.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/f/b/fbf643ccf791cf129362c360fef1d38f3c3efd76.png)

Copy-paste the request body and run against your index in the Kibana dev-tools.

Something like:

```auto
GET myindexpattern/_search
{
 ....request-body-grabbed-from-discover-inspect-tab-that-includes-the-scripted-field...
}

```

---

<div class="post-metadata">

**Author:** ![mats.ahlbom](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@mats.ahlbom](https://discuss.elastic.co/u/mats.ahlbom)\
**Post date:** [October 16, 2020, 9:53am UTC](https://discuss.elastic.co/t/script-exception-runtime-error-in-discover/252218/3 "2020-10-16T09:53:50Z")

</div>

Hi @thomasneirynck,

Thanks for help!

This morning it was working for some hours.  
I think I need to add a NULL check of DOC['comment']. How can I do that? Is it possible to do that in the same IF-statement?

---

<div class="post-metadata">

**Author:** ![matw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matw/32/13913_2.png) [@matw](https://discuss.elastic.co/u/matw)\
**Post date:** [October 16, 2020, 10:00am UTC](https://discuss.elastic.co/t/script-exception-runtime-error-in-discover/252218/4 "2020-10-16T10:00:17Z")

</div>

Hi

You could use `doc.containsKey('keyToCheck')` to check for existence of the field in the doc to prevent the exception.

Best,  
Matthias

---

<div class="post-metadata">

**Author:** ![mats.ahlbom](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@mats.ahlbom](https://discuss.elastic.co/u/mats.ahlbom)\
**Post date:** [October 16, 2020, 10:17am UTC](https://discuss.elastic.co/t/script-exception-runtime-error-in-discover/252218/5 "2020-10-16T10:17:25Z")

</div>

Hi,

I change to

```auto
if ( doc.containsKey('comment') &&
     doc.containsKey('type') &&
     doc.containsKey('change') &&
     doc['type.keyword'].value == "comment-added" &&
     doc['comment.keyword'].value.contains("Code-Review+2") &&
     doc['change.owner.name.keyword'].value == doc['author.name.keyword'].value ) {
    return true;
}
return false;

```

and I think it is working.

---

<div class="post-metadata">

**Author:** ![mats.ahlbom](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@mats.ahlbom](https://discuss.elastic.co/u/mats.ahlbom)\
**Post date:** [October 16, 2020, 11:22am UTC](https://discuss.elastic.co/t/script-exception-runtime-error-in-discover/252218/6 "2020-10-16T11:22:59Z")

</div>

Now I have added more checks and now I think it is working to 100% 🙂

```auto
if ( doc.containsKey('comment') &&
     doc.containsKey('type') &&
     doc.containsKey('change.owner.name') &&
     doc.containsKey('author.name') &&
     doc['type.keyword'].size() > 0 &&
     doc['comment.keyword'].size() > 0 &&
     doc['change.owner.name.keyword'].size() > 0 &&
     doc['author.name.keyword'].size() > 0 &&
     doc['type.keyword'].value == "comment-added" &&
     doc['comment.keyword'].value.contains("Code-Review+2") &&
     doc['change.owner.name.keyword'].value == doc['author.name.keyword'].value ) {
    return true;
}
return false;

```

---

<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:** [November 13, 2020, 11:23am UTC](https://discuss.elastic.co/t/script-exception-runtime-error-in-discover/252218/7 "2020-11-13T11:23:01Z")

</div>

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