Painless script to get date difference

Hi Team,

I want to calculate time difference between two times (incident created time & incident updated time fields).
I'm trying to create a scripted field for this...but getting error while loading.

My scripts looks like this

doc['incident_update_timestamp'].value - doc['wit_cell_16_Value'].value;

I'm getting the following error:

Error: Request to Elasticsearch failed: {"error":{"root_cause":[{"type":"script_exception","reason":"runtime error","script_stack":["return doc['incident_update_timestamp'].value - doc['wit_cell_16_Value'].value;"," ^---- HERE"],"script":"return doc['incident_update_timestamp'].value - doc['wit_cell_16_Value'].value;","lang":"painless"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"soc3d-incidents-v6.0","node":"HMBpWxhwTAuu07-3Uk9vTA","reason":{"type":"script_exception","reason":"runtime error","script_stack":["return doc['incident_update_timestamp'].value - doc['wit_cell_16_Value'].value;"," ^---- HERE"],"script":"return doc['incident_update_timestamp'].value - doc['wit_cell_16_Value'].value;","lang":"painless","caused_by":{"type":"class_cast_exception","reason":"Cannot apply operation to types [org.joda.time.MutableDateTime] and [org.joda.time.MutableDateTime]."}}}]},"status":500}
at https://cicggncyberir.cicindia.in/bundles/kibana.bundle.js?v=8467:229689:36
at Function.Promise.try (https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:94576:20)
at https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:94545:25
at Array.map ()
at Function.Promise.map (https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:94544:28)
at callResponseHandlers (https://cicggncyberir.cicindia.in/bundles/kibana.bundle.js?v=8467:229657:20)
at https://cicggncyberir.cicindia.in/bundles/kibana.bundle.js?v=8467:229179:14
at processQueue (https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:57541:37)
at https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:57585:27
at Scope.$digest (https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:58723:15)
at Scope.$apply (https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:59021:24)
at done (https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:53042:47)
at completeRequest (https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:53268:7)
at XMLHttpRequest.requestLoaded (https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:53196:9)

Can someone help me with this...

I used this post as reference

It sounds like at least one of your fields is a date, not a number. If a field shows up as date type in your mapping, you have to access the value with doc['myFieldNameHere'].value.millis to get the timestamp value as a number.

Also see here Kibana painless- Difference in dates in days

Thanks alot @flash1293...I could able to get the desired outcome

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