# Error with scriptfield in visualization

**URL:** https://discuss.elastic.co/t/error-with-scriptfield-in-visualization/222071
**Category:** Kibana
**Created:** [March 4, 2020, 12:11pm UTC](https://discuss.elastic.co/t/error-with-scriptfield-in-visualization/222071 "2020-03-04T12:11:16Z")
**Posts on this page:** 5
**Page:** 1

<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: [March 4, 2020, 12:11pm UTC](https://discuss.elastic.co/t/error-with-scriptfield-in-visualization/222071/1 "2020-03-04T12:11:16Z")

</div>

Hello, everybody,

i have created a scripted field and whenever i want to visualize it i get an error:

[esaggs] \> Request to Elasticsearch failed: {"error":{"root\_cause":[{"type":"aggregation\_execution\_exception","reason":"Unsupported script value [null], expected a number, date, or boolean"}],"type":"search\_phase\_execution\_exception","reason":"all shards failed","phase":"query","grouped":true,"failed\_shards":[{"shard":0,"index":"uberagent","node":"52tn91DERquwvDXrqkww\_w","reason":{"type":"aggregation\_execution\_exception","reason":"Unsupported script value [null], expected a number, date, or boolean"}}]},"status":500}

The following settings have been set for the Scripted field:

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

If I understand it correctly, will a string be returned to me? If yes why? I have declared it as number.

What I find funny is that I also created a script field last week and put it into a visualization and it worked. If I try it again today it does not work.

I work with the Kibana version 7.5.2

Thanks for your help in advance.

---

<div class="post-metadata">

### Author: ![jbudz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jbudz/32/45922_2.png) [@jbudz](https://discuss.elastic.co/u/jbudz)
#### Post date: [March 4, 2020, 7:57pm UTC](https://discuss.elastic.co/t/error-with-scriptfield-in-visualization/222071/2 "2020-03-04T19:57:47Z")

</div>

This error typically occurs when a script throws an exception. I suspect some documents are sparse, i.e. in some cases that field isn't defined.

Does this work?

```auto
if (doc.containsKey('ProcCPUTimeMs') {
  return doc['ProcCPUTimeMs'].value / 100.0
}

```

---

<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: [March 5, 2020, 11:59am UTC](https://discuss.elastic.co/t/error-with-scriptfield-in-visualization/222071/3 "2020-03-05T11:59:40Z")

</div>

When I use the code I get the following error message:

{  
"root\_cause": [  
{  
"type": "script\_exception",  
"reason": "runtime error",  
"script\_stack": [  
"org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:121)",  
"org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:115)",  
"return doc['ProcCPUTimeMs'].value / 1000.0\n}",  
" ^---- HERE"  
],  
"script": "if (doc.containsKey('ProcCPUTimeMs')) {\n return doc['ProcCPUTimeMs'].value / 1000.0\n}",  
"lang": "painless"  
}  
],  
"type": "search\_phase\_execution\_exception",  
"reason": "all shards failed",  
"phase": "query",  
"grouped": true,  
"failed\_shards": [  
{  
"shard": 0,  
"index": "uberagent",  
"node": "52tn91DERquwvDXrqkww\_w",  
"reason": {  
"type": "script\_exception",  
"reason": "runtime error",  
"script\_stack": [  
"org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:121)",  
"org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:115)",  
"return doc['ProcCPUTimeMs'].value / 1000.0\n}",  
" ^---- HERE"  
],  
"script": "if (doc.containsKey('ProcCPUTimeMs')) {\n return doc['ProcCPUTimeMs'].value / 1000.0\n}",  
"lang": "painless",  
"caused\_by": {  
"type": "illegal\_state\_exception",  
"reason": "A document doesn't have a value for a field! Use doc[].size()==0 to check if a document is missing a field!"  
}  
}  
}  
]  
}

As you say, in many documents this field does not exist or it is empty. When I check the field with the following command: if (doc['ProcCPUTimeMs'].size() ==0) return ''"; the script works but I get the above error when I try to visualize the data.

Or do I always have to check in the script if the field ProcCPUTimeMs is present?

---

<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: [March 9, 2020, 10:03pm UTC](https://discuss.elastic.co/t/error-with-scriptfield-in-visualization/222071/4 "2020-03-09T22:03:33Z")

</div>

Does anyone have a solution or suggestions?

---

<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: [April 6, 2020, 10:03pm UTC](https://discuss.elastic.co/t/error-with-scriptfield-in-visualization/222071/5 "2020-04-06T22:03:36Z")

</div>

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