# Using scripted fields from Kibana raises a script exception in ES

**URL:** <https://discuss.elastic.co/t/using-scripted-fields-from-kibana-raises-a-script-exception-in-es/62934>\
**Category:** Elasticsearch\
**Created:** [October 13, 2016, 12:05pm UTC](https://discuss.elastic.co/t/using-scripted-fields-from-kibana-raises-a-script-exception-in-es/62934 "2016-10-13T12:05:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leryan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leryan/32/12479_2.png) [@Leryan](https://discuss.elastic.co/u/Leryan)\
**Post date:** [October 13, 2016, 12:05pm UTC](https://discuss.elastic.co/t/using-scripted-fields-from-kibana-raises-a-script-exception-in-es/62934/1 "2016-10-13T12:05:18Z")

</div>

Hi,

I'm using ES 2.4.1 with Oracle JRE 8, on CentOS 7.

When using a scripted field in Kibana, if the field used for the computation doesn't exists in the time range, ES raises an exception, failing the query:

```auto
Discover: Field [apache_reqtime] used in expression does not exist in mappings

Error: Request to Elasticsearch failed: {"error":{"root_cause":[{"type":"script_exception","reason":"Field [apache_reqtime] used in expression does not exist in mappings"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"logstash-2016.10.10","node":"DUXanaGfSretbEe7OcVVaA","reason":{"type":"script_exception","reason":"Error during search with inline script [doc['apache_reqtime'] / 1000] using lang [expression]","caused_by":{"type":"script_exception","reason":"Field [apache_reqtime] used in expression does not exist in mappings"}}}]}}

```

The scripted field is simply dividing an `apache_reqtime` by 1000. This field isn't always present in the selected timerange from kibana.

Am I doing something wrong with the data I store into ES? Should i do an extra check into the scripted field? Thanks.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [October 14, 2016, 3:04pm UTC](https://discuss.elastic.co/t/using-scripted-fields-from-kibana-raises-a-script-exception-in-es/62934/2 "2016-10-14T15:04:14Z")

</div>

One workaround would be to add this field to the mappings of your indices (it does not require reindexing). It can even be added to an index template so that new indices automatically get this field even if no documents contain it.

---

<div class="post-metadata">

**Author:** ![Leryan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leryan/32/12479_2.png) [@Leryan](https://discuss.elastic.co/u/Leryan)\
**Post date:** [October 15, 2016, 8:49pm UTC](https://discuss.elastic.co/t/using-scripted-fields-from-kibana-raises-a-script-exception-in-es/62934/3 "2016-10-15T20:49:32Z")

</div>

Oh thanks, I think you're right:

```auto
curl -XPUT localhost:9200/logstash-2016.10.15/_mapping/log -d '{"properties":{"apache_reqtime":{"type": "long"}}}

```

This avoid the error, but then the result is "corrupted" since the scripted field is always 0. For that, adding a query to the visualization makes the job: `{"range":{"apache_reqtime":{"gt":0}}}`

---

<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:** [July 5, 2017, 10:12pm UTC](https://discuss.elastic.co/t/using-scripted-fields-from-kibana-raises-a-script-exception-in-es/62934/4 "2017-07-05T22:12:00Z")

</div>


