Painless script to find the difference between two timestamp values

Hi All,
I am trying to define a scripted field in ES 7.17 scope where the difference between two timestamp field values need to be defined.
I did looked into the painless documentation, but could not exactly find some matching reference.
Could someone please share any sample painless script to find the difference between two timestamp field values in minutes?

Thanks
Venkatesh

Hi @rvadiga

Maybe this answer help you to start:

I tried with the below option

{
"script": {
"script": {
"inline": "doc['col1'].value.toInstant().toEpochMilli() - doc['col2'].value.toInstant().toEpochMilli() > params.difference",
"lang": "painless",
"params": {
"difference": 60000
}
}
}
}

Do you suggest whether it is correct definition to find minute level difference.

Regards
Venkatesh

Please use the format tools code for help visualize the script.

Do you want something like this:

POST teste/_doc
{
  "col1": "2022-06-16T09:00:00.000",
  "col2": "2022-06-16T09:09:00.00"
}

POST teste/_doc
{
  "col1": "2022-06-16T09:00:00.000",
  "col2": "2022-06-16T0:11:00.00"
}

GET teste/_search
{
  "_source": [
    "*"
  ],
  "script_fields": {
    "time_diff": {
      "script": {
        "params": {
          "difference": 10
        },
        "source": """
          def diffInMs = doc['col2'].value.getMillis() - doc['col1'].value.getMillis();
          def minutes = (diffInMs / 1000) / 60;
          return minutes > params.difference;
      """
      }
    }
  }
}

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