I have data with two date field like this
"@buildTimestamp": "2020-12-01T18:55:34.895-0800",
"@timestamp": "2020-12-02T03:02:16.723Z"
I'd like to use ingest pipeline script processor to get the difference.
How can I converse this two to the same format, then do the comparison?
I cannot get it right
I tried something like this
POST _ingest/pipeline/_simulate
{
"pipeline": {
"processors": [
{
"script": {
"lang": "painless",
"source": """
def test;
test = ChronoUnit.MILLIS.between(ZonedDateTime.parse(ctx['@buildTimestamp']), ZonedDateTime.parse(ctx['@timestamp']))/1000;
"""
}
}
]
},
"docs": [
{
"_index": "test-2020.12",
"_type": "_doc",
"_id": "ojlmIXYBFr9SJ001ii0C",
"_source": {
"@buildTimestamp": "2020-12-01T18:55:34.895-0800",
"@timestamp": "2020-12-02T03:02:16.723Z"
}
}
]
}
but the error shows
{
"docs" : [
{
"error" : {
"root_cause" : [
{
"type" : "exception",
"reason" : "java.lang.IllegalArgumentException: ScriptException[runtime error]; nested: DateTimeParseException[Text '2020-12-01T18:55:34.895-0800' could not be parsed, unparsed text found at index 26];",
"header" : {
"processor_type" : "script"
}
}
],
"type" : "exception",
"reason" : "java.lang.IllegalArgumentException: ScriptException[runtime error]; nested: DateTimeParseException[Text '2020-12-01T18:55:34.895-0800' could not be parsed, unparsed text found at index 26];",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "ScriptException[runtime error]; nested: DateTimeParseException[Text '2020-12-01T18:55:34.895-0800' could not be parsed, unparsed text found at index 26];",
"caused_by" : {
"type" : "script_exception",
"reason" : "runtime error",
"script_stack" : [
"java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)",
"java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1951)",
"java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:598)",
"java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:583)",
"test = ChronoUnit.MILLIS.between(ZonedDateTime.parse(ctx['@buildTimestamp']), ZonedDateTime.parse(ctx['@timestamp']))/1000;",
" ^---- HERE"
],
"script" : " def test;\n test = ChronoUnit.MILLIS.between(ZonedDateTime.parse(ctx['@buildTimestamp']), ZonedDateTime.parse(ctx['@timestamp']))/1000;",
"lang" : "painless",
"caused_by" : {
"type" : "date_time_parse_exception",
"reason" : "Text '2020-12-01T18:55:34.895-0800' could not be parsed, unparsed text found at index 26"
}
}
},
"header" : {
"processor_type" : "script"
}
}
}
]
}