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 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)
at 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)
at callResponseHandlers (https://elawview.elaw.com.br/bundles/commons.bundle.js:3:1387778)
at 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)
at 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)

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!

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.