Created
April 26, 2022 17:17
-
-
Save Lokey92/f9b60f00a8f457bedf6f943e21a87837 to your computer and use it in GitHub Desktop.
[TUTORIAL] Runtime Fields - Date Diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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