Skip to content

Instantly share code, notes, and snippets.

@Lokey92
Created April 26, 2022 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lokey92/f9b60f00a8f457bedf6f943e21a87837 to your computer and use it in GitHub Desktop.
Save Lokey92/f9b60f00a8f457bedf6f943e21a87837 to your computer and use it in GitHub Desktop.
[TUTORIAL] Runtime Fields - Date Diff
# Runtime Painless Example - Retrieve two date fields, diff them, then output a DDHHMMSS time.
GET .ds-ilm-history-*/_search
{
"fields": ["date.diff.create-step"],
"runtime_mappings": {
"date.diff.create-step": {
"type": "keyword",
"script": {
"source": """
ZonedDateTime step = ZonedDateTime.ofInstant(doc['state.step_time'].value.toInstant(), ZoneId.of('Z'));
ZonedDateTime create = ZonedDateTime.ofInstant(doc['state.creation_date'].value.toInstant(), ZoneId.of('Z'));
def days = ChronoUnit.DAYS.between(create, step);
def hours = ChronoUnit.HOURS.between(create, step) % 24;
def minutes = ChronoUnit.MINUTES.between(create, step) % (24*60);
def seconds = ChronoUnit.SECONDS.between(create, step) % 60;
emit(days + ' days ' + hours + ' hours ' + minutes + ' minutes ' + seconds + ' seconds')
"""
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment