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

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 Function.Promise.try (
at ()
at (
at callResponseHandlers (
at processQueue (
at Scope.$digest (

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?

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

Hi Lukas,

I've selected Date - default

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?)

It is multiple indices, but the others indices don't have these fields.

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