# Created script field successfully but error occurred in discover

**URL:** <https://discuss.elastic.co/t/created-script-field-successfully-but-error-occurred-in-discover/228813>\
**Category:** Kibana\
**Created:** [April 20, 2020, 8:38am UTC](https://discuss.elastic.co/t/created-script-field-successfully-but-error-occurred-in-discover/228813 "2020-04-20T08:38:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Echo\_yu](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@Echo\_yu](https://discuss.elastic.co/u/Echo_yu)\
**Post date:** [April 20, 2020, 8:38am UTC](https://discuss.elastic.co/t/created-script-field-successfully-but-error-occurred-in-discover/228813/1 "2020-04-20T08:38:31Z")

</div>

Hi, I created a script field with the type of date successfully but when I search data in discover something went wrong . And I cannot use my script field in aggregation either. My script field create by `if(doc['finished'].value.getHour()>=16) { Instant.ofEpochMilli(doc['finished'].value.getMillis()+8*60*60*1000); } else { Instant.ofEpochMilli(doc['finished'].value.getMillis()); }`. And here is the error below:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/a/4/a4da52ce8ad087b4a8d1410a6d5f063753510ee1.png)  
Detail of exception:  
`Request to Elasticsearch failed: {"error":{"root_cause":[{"type":"script_exception","reason":"runtime error","script_stack":["org.elasticsearch.index.fielddata.ScriptDocValues$Dates.get(ScriptDocValues.java:160)","org.elasticsearch.index.fielddata.ScriptDocValues$Dates.getValue(ScriptDocValues.java:154)","if(doc['finished'].value.getHour()>=16)\n{\n "," ^---- HERE"],"script":"if(doc['finished'].value.getHour()>=16)\n{\n Instant.ofEpochMilli(doc['finished'].value.getMillis()+8*60*60*1000);\n}\nelse\n{\n Instant.ofEpochMilli(doc['finished'].value.getMillis());\n}","lang":"painless"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"bp-session-log-b","node":"VlTdaqFERuaqpqtJyFR8Sw","reason":{"type":"script_exception","reason":"runtime error","script_stack":["org.elasticsearch.index.fielddata.ScriptDocValues$Dates.get(ScriptDocValues.java:160)","org.elasticsearch.index.fielddata.ScriptDocValues$Dates.getValue(ScriptDocValues.java:154)","if(doc['finished'].value.getHour()>=16)\n{\n "," ^---- HERE"],"script":"if(doc['finished'].value.getHour()>=16)\n{\n Instant.ofEpochMilli(doc['finished'].value.getMillis()+8*60*60*1000);\n}\nelse\n{\n Instant.ofEpochMilli(doc['finished'].value.getMillis());\n}","lang":"painless","caused_by":{"type":"illegal_state_exception","reason":"A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!"}}}]},"status":400}`  
Looking forward to your help，Thanks！

---

<div class="post-metadata">

**Author:** ![dosant](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dosant/32/64489_2.png) [@dosant](https://discuss.elastic.co/u/dosant)\
**Post date:** [April 20, 2020, 10:38am UTC](https://discuss.elastic.co/t/created-script-field-successfully-but-error-occurred-in-discover/228813/2 "2020-04-20T10:38:00Z")

</div>

Hi @Echo_yu,

I haven't tried anything yet, but looking at the error:

> [@Echo\_yu](#):
>
> "reason":"A document doesn't have a value for a field! Use doc[].size()==0 to check if a document is missing a field!"}}}]},

Looks like there is a null pointer exception and some of the docs are missing `finished` field.  
Have you tried recommendation from error to check if the field is present first and then access it?

---

<div class="post-metadata">

**Author:** ![Echo\_yu](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@Echo\_yu](https://discuss.elastic.co/u/Echo_yu)\
**Post date:** [April 20, 2020, 11:02am UTC](https://discuss.elastic.co/t/created-script-field-successfully-but-error-occurred-in-discover/228813/3 "2020-04-20T11:02:50Z")

</div>

Hi， I have tried this recommendation and I have already dealt with this problem. However I got a new problem of script field. Why did I aggregate the new script field which was named `finial_date` in kibana's dev tool but always returned null? My code is：  
`GET /bp-session-log-all/_search`  
`{`  
` "query": {`  
` "bool": {`  
` "must": [`  
` {"match": {`  
` "workqueue_name.keyword": "STR_WQ_1.2 Process STC"`  
` }},`  
` {"match": {`  
` "txn_success": 0}}]}},`  
`"aggs": {`  
`"datetime": {`  
` "date_range": {`  
`"field": "finial_date",`  
`"ranges": [`  
`{`  
` "from": "now-1d/d",`  
`"to": "now/d"`  
` }]}}}}`  
Looking forwarding to your help. Thanks!

---

<div class="post-metadata">

**Author:** ![dosant](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dosant/32/64489_2.png) [@dosant](https://discuss.elastic.co/u/dosant)\
**Post date:** [April 20, 2020, 11:46am UTC](https://discuss.elastic.co/t/created-script-field-successfully-but-error-occurred-in-discover/228813/4 "2020-04-20T11:46:59Z")

</div>

@Echo_yu, scripted field is created on index pattern level which is part of Kibana, and not Elasticsearch.

If you want to see aggregation with scripted field in dev tools, you'd have to manually duplicate your script inside that aggregation. There is a documented example [using a script for a sum aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html#_script_10) which may help. To avoid duplication, you could also [store the script](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-using.html#modules-scripting-stored-scripts) and reference in both locations by name. - [How to use a script field inside Aggregate?](https://discuss.elastic.co/t/how-to-use-a-script-field-inside-aggregate/191190/2)

But this scripted field should be available for aggregations in Kibana's visualize. So you should be able to create a visualisation on that field. Internally Kibana will use your script inside the aggregation in request to Elasticsearch.

Hope this is helpful

---

<div class="post-metadata">

**Author:** ![Echo\_yu](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@Echo\_yu](https://discuss.elastic.co/u/Echo_yu)\
**Post date:** [April 21, 2020, 8:14am UTC](https://discuss.elastic.co/t/created-script-field-successfully-but-error-occurred-in-discover/228813/5 "2020-04-21T08:14:53Z")

</div>

Thanks for your help, I will try your recommendation later 🥰

---

<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:** [May 19, 2020, 8:14am UTC](https://discuss.elastic.co/t/created-script-field-successfully-but-error-occurred-in-discover/228813/6 "2020-05-19T08:14:57Z")

</div>

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