# I am not able to make my scripted field work. Can someone help me to understand what am I doing wrong here

**URL:** <https://discuss.elastic.co/t/i-am-not-able-to-make-my-scripted-field-work-can-someone-help-me-to-understand-what-am-i-doing-wrong-here/222174>\
**Category:** Kibana\
**Created:** [March 4, 2020, 10:57pm UTC](https://discuss.elastic.co/t/i-am-not-able-to-make-my-scripted-field-work-can-someone-help-me-to-understand-what-am-i-doing-wrong-here/222174 "2020-03-04T22:57:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![preyansh\_shah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/preyansh_shah/32/63837_2.png) [@preyansh\_shah](https://discuss.elastic.co/u/preyansh_shah)\
**Post date:** [March 4, 2020, 10:57pm UTC](https://discuss.elastic.co/t/i-am-not-able-to-make-my-scripted-field-work-can-someone-help-me-to-understand-what-am-i-doing-wrong-here/222174/1 "2020-03-04T22:57:40Z")

</div>

Kibana Version: 7.5.0 management

I wrote a scripted field to find the difference between two field in a document:

if (doc['page\_details.Compliance.ui'].size() == 0) { return ''; } else { return doc['page\_details.Compliance.ui'].value - doc['page\_details.Compliance.api'].value; }

Following is one of the document data example:  
{"\_index":"ui\_page\_load\_detail","\_type":"\_doc","\_id":"81y2p3ABMtQ8ctK1rjhp","\_version":1,"\_score":null,"\_source":{"ui\_version":"19fce8d500282928d76cca2f371ec71ff7735ba9","date":"2020-03-04T22:42:12.151203","page\_details":{"Alerts":{"ui":3.6570801734924316,"api":3.8149843215942383},"Licensing":{"ui":0.5297873020172119,"api":0.16589069366455078},"Account Groups":{"ui":0.455629825592041,"api":0.1664748191833496},"Cloud Accounts":{"ui":1.037529706954956,"api":0.1677558422088623},"Inventory":{"ui":1.148608922958374,"api":0.19062137603759766},"Compliance":{"ui":0.7704780101776123,"api":0.19397377967834473},"Policies":{"ui":0.8386428356170654,"api":0.6650028228759766}},"env":"app2.eu","customer":"PANW-dev"},"fields":{"date":["2020-03-04T22:42:12.151Z"]},"sort":[1583361732151]}

For some reason I keep on getting the following error when I try to search the scripted field in discover:

{  
"took": 38,  
"timed\_out": false,  
"\_shards": {  
"total": 2,  
"successful": 1,  
"skipped": 0,  
"failed": 1,  
"failures": [  
{  
"shard": 0,  
"index": "ui\_page\_load\_detail",  
"node": "WM3GmVorQbSONmqE776AEA",  
"reason": {  
"type": "script\_exception",  
"reason": "runtime error",  
"script\_stack": [  
"org.elasticsearch.index.fielddata.ScriptDocValues$Doubles.get(ScriptDocValues.java:249)",  
"org.elasticsearch.index.fielddata.ScriptDocValues$Doubles.getValue(ScriptDocValues.java:243)",  
"return doc['page\_details.Compliance.ui'].value - doc['page\_details.Compliance.api'].value;\n}",  
" ^---- HERE"  
],  
"script": "if (doc['page\_details.Compliance.ui'].size() == 0) {\n return '';\n}\nelse {\n return doc['page\_details.Compliance.ui'].value - doc['page\_details.Compliance.api'].value;\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!"  
}  
}  
}  
]  
},

Can someone help me in understanding what am I doing wrong here?

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [March 5, 2020, 8:46am UTC](https://discuss.elastic.co/t/i-am-not-able-to-make-my-scripted-field-work-can-someone-help-me-to-understand-what-am-i-doing-wrong-here/222174/2 "2020-03-05T08:46:31Z")

</div>

Is it possible `page_details.Compliance.api` is undefined? Your script is only checking for `page_details.Compliance.ui`, so if one field is defined and the other isn't, it would explain this error.

Try checking for the existence of both fields

---

<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 2, 2020, 8:46am UTC](https://discuss.elastic.co/t/i-am-not-able-to-make-my-scripted-field-work-can-someone-help-me-to-understand-what-am-i-doing-wrong-here/222174/3 "2020-04-02T08:46:37Z")

</div>

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