Found a similar question here: Painless dates in update script
I've rewritten my script to cache any Date
fields when they are initialized/updated and assume that they are String
otherwise.
I'm really hoping there is an alternative to this, but in case this is the expected behavior here is one solution for anyone else who comes across this problem:
//Copy doc source to local variable with shorter name
def docSrc = ctx._source;
// Initialize if new document
if("create".equals(ctx.op)){
docSrc.eventCount = 1;
docSrc.duplicateEventsSkipped = 0;
}
// Cache date fields.
def activityInstant = null;
if (docSrc.activity != null) {
activityInstant = Instant.parse(docSrc.activity);
}
def otherDateField = null;
if (docSrc.otherDateField != null) {
otherDateField = Instant.parse(docSrc.activity);
}
// Consolidate latest batch of events
for (event in params.events) {
def timestamp = Instant.parse( event['@timestamp'] );
// Skip duplicate
if (activityInstant != null && timestamp.isBefore(activityInstant)) {
docSrc.duplicateEventsSkipped ++;
continue;
}
docSrc.eventCount ++;
docSrc.activity = Date.from(timestamp);
docSrc.otherDateField = Date.from(Instant.parse(event['otherDateField']))
}
// Compute any additional data using the cached dates rather than the values updated in ctx._source.