# Painless Cannot cast from \[java.time.LocalDateTime\] to \[double\]

**URL:** <https://discuss.elastic.co/t/painless-cannot-cast-from-java-time-localdatetime-to-double/198776>\
**Category:** Kibana\
**Created:** [September 9, 2019, 7:54pm UTC](https://discuss.elastic.co/t/painless-cannot-cast-from-java-time-localdatetime-to-double/198776 "2019-09-09T19:54:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BrunoSdeMenezes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brunosdemenezes/32/46413_2.png) [@BrunoSdeMenezes](https://discuss.elastic.co/u/BrunoSdeMenezes)\
**Post date:** [September 9, 2019, 7:54pm UTC](https://discuss.elastic.co/t/painless-cannot-cast-from-java-time-localdatetime-to-double/198776/1 "2019-09-09T19:54:09Z")

</div>

Hello all,  
I'm trying to create a scripted field that joins two fields into one. If any of fields is null, it returns the minimal LocalDateTime.  
The preview runs everything right, but when I try to order in the discovery, I get this message:

> Error: Request to Elasticsearch failed: {"error":{"root\_cause":[{"type":"class\_cast\_exception","reason":"class\_cast\_exception: Cannot cast from [java.time.LocalDateTime] to [double]."}],"type":"search\_phase\_execution\_exception","reason":"all shards failed","phase":"query","grouped":true,"failed\_shards":[{"shard":0,"index":"ca\_process","node":"EqfbZ7UtS8aorpsDs5pF\_A","reason":{"type":"script\_exception","reason":"compile error","script\_stack":["... )), timeZone);\n}\n\nreturn data\_dem;"," ^---- HERE"],"script":"LocalDateTime data\_dem = LocalDateTime.MIN;\nZoneId timeZone = ZoneId.of('Z');\nif(doc['data\_dem\_1'].size() \> 0){\n data\_dem = LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['data\_dem\_1'].value.getMillis()), timeZone);\n}else if(doc['data\_dem\_2'].size() \> 0){\n data\_dem = LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['data\_dem\_2'].value.getMillis()), timeZone);\n}\n\nreturn data\_dem;","lang":"painless","caused\_by":{"type":"class\_cast\_exception","reason":"class\_cast\_exception: Cannot cast from [java.time.LocalDateTime] to [double]."}}}],"caused\_by":{"type":"class\_cast\_exception","reason":"class\_cast\_exception: Cannot cast from [java.time.LocalDateTime] to [double]."}},"status":400}  
> at [https://elawview.elaw.com.br/bundles/commons.bundle.js:3:1388766](https://elawview.elaw.com.br/bundles/commons.bundle.js:3:1388766)  
> at Function.Promise.try ([https://elawview.elaw.com.br/bundles/commons.bundle.js:3:1065792](https://elawview.elaw.com.br/bundles/commons.bundle.js:3:1065792))  
> at [https://elawview.elaw.com.br/bundles/commons.bundle.js:3:1065161](https://elawview.elaw.com.br/bundles/commons.bundle.js:3:1065161)  
> at Array.map ()  
> at Function.Promise.map ([https://elawview.elaw.com.br/bundles/commons.bundle.js:3:1065119](https://elawview.elaw.com.br/bundles/commons.bundle.js:3:1065119))  
> at callResponseHandlers ([https://elawview.elaw.com.br/bundles/commons.bundle.js:3:1387778](https://elawview.elaw.com.br/bundles/commons.bundle.js:3:1387778))  
> at [https://elawview.elaw.com.br/bundles/commons.bundle.js:3:1369738](https://elawview.elaw.com.br/bundles/commons.bundle.js:3:1369738)  
> at processQueue ([https://elawview.elaw.com.br/built\_assets/dlls/vendors.bundle.dll.js:427:199687](https://elawview.elaw.com.br/built_assets/dlls/vendors.bundle.dll.js:427:199687))  
> at [https://elawview.elaw.com.br/built\_assets/dlls/vendors.bundle.dll.js:427:200650](https://elawview.elaw.com.br/built_assets/dlls/vendors.bundle.dll.js:427:200650)  
> at Scope.$digest ([https://elawview.elaw.com.br/built\_assets/dlls/vendors.bundle.dll.js:427:210412](https://elawview.elaw.com.br/built_assets/dlls/vendors.bundle.dll.js:427:210412))

This is the script I'm using:

> LocalDateTime data\_dem = LocalDateTime.MIN;  
> ZoneId timeZone = ZoneId.of('Z');  
> if(doc['data\_dem\_1'].size() \> 0){  
> data\_dem = LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['data\_dem\_1'].value.getMillis()), timeZone);  
> }else if(doc['data\_dem\_2'].size() \> 0){  
> data\_dem = LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['data\_dem\_2'].value.getMillis()), timeZone);  
> }
> 
> return data\_dem;

Anyone has seen this before?  
Thanks!

---

<div class="post-metadata">

**Author:** ![lukas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas/32/6812_2.png) [@lukas](https://discuss.elastic.co/u/lukas)\
**Post date:** [September 12, 2019, 5:24pm UTC](https://discuss.elastic.co/t/painless-cannot-cast-from-java-time-localdatetime-to-double/198776/2 "2019-09-12T17:24:27Z")

</div>

Hmm, what's the `type` you've selected for your scripted field?

---

<div class="post-metadata">

**Author:** ![BrunoSdeMenezes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brunosdemenezes/32/46413_2.png) [@BrunoSdeMenezes](https://discuss.elastic.co/u/BrunoSdeMenezes)\
**Post date:** [September 12, 2019, 6:29pm UTC](https://discuss.elastic.co/t/painless-cannot-cast-from-java-time-localdatetime-to-double/198776/3 "2019-09-12T18:29:02Z")

</div>

Hi Lukas,

I've selected Date - default

---

<div class="post-metadata">

**Author:** ![lukas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas/32/6812_2.png) [@lukas](https://discuss.elastic.co/u/lukas)\
**Post date:** [September 12, 2019, 6:57pm UTC](https://discuss.elastic.co/t/painless-cannot-cast-from-java-time-localdatetime-to-double/198776/4 "2019-09-12T18:57:36Z")

</div>

Hmm. Does your data include multiple indices for which those fields are different types? (One with a date and another with a number or something?)

---

<div class="post-metadata">

**Author:** ![BrunoSdeMenezes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brunosdemenezes/32/46413_2.png) [@BrunoSdeMenezes](https://discuss.elastic.co/u/BrunoSdeMenezes)\
**Post date:** [September 12, 2019, 7:42pm UTC](https://discuss.elastic.co/t/painless-cannot-cast-from-java-time-localdatetime-to-double/198776/5 "2019-09-12T19:42:12Z")

</div>

It is multiple indices, but the others indices don't have these 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:** [October 10, 2019, 7:52pm UTC](https://discuss.elastic.co/t/painless-cannot-cast-from-java-time-localdatetime-to-double/198776/6 "2019-10-10T19:52:51Z")

</div>

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