# Kibana scripted field (bad request error)

**URL:** <https://discuss.elastic.co/t/kibana-scripted-field-bad-request-error/238767>\
**Category:** Kibana\
**Created:** [June 26, 2020, 1:45am UTC](https://discuss.elastic.co/t/kibana-scripted-field-bad-request-error/238767 "2020-06-26T01:45:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![thanirin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thanirin/32/71146_2.png) [@thanirin](https://discuss.elastic.co/u/thanirin)\
**Post date:** [June 26, 2020, 1:45am UTC](https://discuss.elastic.co/t/kibana-scripted-field-bad-request-error/238767/1 "2020-06-26T01:45:30Z")

</div>

Hi, so I added a scripted field in one of the index pattern. I am pretty sure the code should work fine.

```auto
return doc['sequence'].value % 2

```

When I previewed the results, it works fine. However, when I tried to view it in Discover, it said "error loading data, [script\_exception] runtime error"

Below are the full error descriptions

> Error: Bad Request  
> at Fetch.\_callee3$ ([http://localhost:5601/bundles/commons.bundle.js:3:4001197](http://localhost:5601/bundles/commons.bundle.js:3:4001197))  
> at l ([http://localhost:5601/bundles/kbn-ui-shared-deps/kbn-ui-shared-deps.js:288:970406](http://localhost:5601/bundles/kbn-ui-shared-deps/kbn-ui-shared-deps.js:288:970406))  
> at Generator.\_invoke ([http://localhost:5601/bundles/kbn-ui-shared-deps/kbn-ui-shared-deps.js:288:970159](http://localhost:5601/bundles/kbn-ui-shared-deps/kbn-ui-shared-deps.js:288:970159))  
> at Generator.forEach.e. [as next] ([http://localhost:5601/bundles/kbn-ui-shared-deps/kbn-ui-shared-deps.js:288:970763](http://localhost:5601/bundles/kbn-ui-shared-deps/kbn-ui-shared-deps.js:288:970763))  
> at asyncGeneratorStep ([http://localhost:5601/bundles/commons.bundle.js:3:3994720](http://localhost:5601/bundles/commons.bundle.js:3:3994720))  
> at \_next ([http://localhost:5601/bundles/commons.bundle.js:3:3995031](http://localhost:5601/bundles/commons.bundle.js:3:3995031))

---

<div class="post-metadata">

**Author:** ![thanirin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thanirin/32/71146_2.png) [@thanirin](https://discuss.elastic.co/u/thanirin)\
**Post date:** [June 26, 2020, 1:48am UTC](https://discuss.elastic.co/t/kibana-scripted-field-bad-request-error/238767/2 "2020-06-26T01:48:17Z")

</div>

This is the said scripted field

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

And this is some of the preview results. They're correct as well.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/8/e/8e791dad7dd69482c06fbdd784134ea33afc2664.png)

---

<div class="post-metadata">

**Author:** ![thanirin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thanirin/32/71146_2.png) [@thanirin](https://discuss.elastic.co/u/thanirin)\
**Post date:** [June 26, 2020, 4:09am UTC](https://discuss.elastic.co/t/kibana-scripted-field-bad-request-error/238767/3 "2020-06-26T04:09:28Z")

</div>

It seems that not every log has the same field. I have added a condition to check whether there is that field and this is solved.

---

<div class="post-metadata">

**Author:** ![Anton91](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@Anton91](https://discuss.elastic.co/u/Anton91)\
**Post date:** [July 14, 2020, 1:45pm UTC](https://discuss.elastic.co/t/kibana-scripted-field-bad-request-error/238767/4 "2020-07-14T13:45:49Z")

</div>

Hey @thanirin,

I have the same problems. Could you tell me exactly what the condition is?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![thanirin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thanirin/32/71146_2.png) [@thanirin](https://discuss.elastic.co/u/thanirin)\
**Post date:** [July 14, 2020, 3:45pm UTC](https://discuss.elastic.co/t/kibana-scripted-field-bad-request-error/238767/5 "2020-07-14T15:45:18Z")

</div>

Hi @Anton91,

In my case, every log has the same field called "type", and I also know that a certain type has the field no other type has. That's why I could add a condition to filter the type I want first. For example...

```auto
if(doc['type.keyword'].value.contains("the_Type_I_Want")) {
    return doc['filed_not_avail_every_type'].value % 2
}

```

However, if you don't have a field that can differentiate the log, you might be able check if the interested field is `null` or not. Although, I'm not sure if it will work because I haven't tried it.

* * *

edit: I found out that you can use `_exists_:the_field_name` to check if a field is not null.

---

<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:** [August 11, 2020, 3:45pm UTC](https://discuss.elastic.co/t/kibana-scripted-field-bad-request-error/238767/6 "2020-08-11T15:45:25Z")

</div>

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