How to calculate date diffs using ingest node

Hi
I have a basic date diff between 2 dates to do and display it as weeks.
If i use the Kibana scripted fields, it works fine using the following syntax :

Math.round((doc['last_detected'].value.toInstant().toEpochMilli() - doc['first_detected'].value.toInstant().toEpochMilli())/((3600000*24))/7)

However, I would like to have it calculated during the document ingestion but i can not figure which is the syntax ?

Any guess ?

The doc syntax to access field only works at search or aggregation time. To access fields in an ingest processor, you can use the ctx["field_name"] syntax.

Something like the following processor should work:

    {
      "script": {
        "source": """
  if(ctx.containsKey("first_detected") && ctx.containsKey("last_detected")) {
    ZonedDateTime first = ZonedDateTime.parse(ctx["first_detected"]);
    ZonedDateTime last = ZonedDateTime.parse(ctx["last_detected"]);
    ctx.diff = ChronoUnit.MILLIS.between(first, last);
  }
"""
      }
    }
1 Like

Hi
Thanks a lot.
Regards

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