Painless script error in getting sameday documents

Hi All,

We are pulling data into elasticsearch using logstash http_poller and our datasource is servicenow.

We have so many incidents created in a day, i'm trying to write a painless script get get the same day closure tickets for which i'm using the below script, but its throwing an unexceptional error.

if ((doc['result.resolved_at'].date.dayOfYear - doc['result.sys_created_on'].date.dayOfYear) ==0)
{
return 0;
} else {
return 1;
}

We are using version 7.6.2

Any advice please.

Thanks
Gautham

Try this may help

if (((doc['result.resolved_at'].value.toInstant().toEpochMilli() - doc['result.sys_created_on'].value.toInstant().toEpochMilli()) / 1000 * 60 * 60 *24) == 0){
	return 0;
} else {
	return 1;
}
1 Like

@ylasri

Its working great.. Thanks .....

If i need to calculate for month then should i need to multiply by 12

Thanks
Gautham

@ylasri I also need to write a condition to check the resolved at, because it'll be empty sometimes, any idea in adding that condition along with the provided code.

Thanks
Gautham

Use something like this to check if field exist in the doc

if (!doc.containsKey('myfield') || doc['myfield'].empty) { return "unavailable" } else { return doc['myfield'].value }

2 Likes

@ylasri Got it its working perfectly.....

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