Painless script error

Hi, I have a script below -
if(doc['not_shipped_shortfall'].value < 0) { return doc['not_shipped_shortfall'].value; }else{ return 0; }

when I click 'preview results' I see my desired result and looks like the script is working fine. However, when I try to use this metric in the visualizer I get the following error -

if(doc['not_shipped_shortfall'].value < 0.0) { 
                               ^---- HERE

I'd greatly appreciate if you can point me in the right direction.

Hey @Surya_Raviraj ,
First, I created a test index with only one field to test:

PUT test
  "mappings": {
    "properties": {
      "float_field": {
        "type": "float"

POST test/_doc/1
  "float_field": 1.2
POST test/_doc/2
  "float_field": -1.2

Then copied/pasted your code as it is and worked in the scripted field in the index pattern:

if(doc['float_field'].value < 0) { 
    return doc['float_field'].value; 
else {
    return 0; 

the script above worked for me in the previou results tab

Can you please tell us additional information: what elastic version are you using? and also paste the whole stack trace, there is a reason field at the bottom of the error.
and I can use the new_field created in a vizualisation:

FYI I tested that on elastic 8.0.1.

Thanks Marone, I am currently using elastic version 7.10.
How can I find the whole stack trace? The error message I pasted was copied when I try to use it in a visualization.

Something to also note that is that the field I am working with has a mapping type of double. Fortunately, all the numbers in that field are/can only whole numbers. Not sure if that changes anything.

I updated the mapping to double and still working.

For the whole stacktrace I thought it was printed from Elasticsearch side, but since you got it when trying to make the vizualisation, the code is executed on the kibana side.

Do you have the new field processed on Discover tab? Like this:

Yes, I am able to see the values on the discover tab. I am not able to use this field on my visualization though. That's what is confusing me. Any hints or tips that you might have?

That's confusing me as well, maybe if you can update your elastic to a newer version? or try to create an issue on Issues · elastic/kibana · GitHub

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