How to increase a date by e.g. 1 day using Painless script?

Just starting to use Painless as excercise before certification.

Am wondering how to attack the problem of updating/increasing a date field by using a Painless script?
I know how to for example increase a simple counter:

POST test/_update/1
{
  "script":
  {
    "source": "ctx._source.counter += params.count",
    "lang": "painless",
    "params": {
      "count": 1
    }
  }
}

, but don´t really get it when it comes to date formats.

See extract from my sample mapping below, how would I go about to update any of those by 1 day?

    "revision_date" : {
      "type" : "date"
    },
    "revision_date_epoch" : {
      "type" : "date",
      "format" : "epoch_second"
    },
    "revision_date_epochm" : {
      "type" : "date",
      "format" : "epoch_millis"
    }

Been trying according to posts that I´ve found but hope to get the strategy explained here to get going.

Thx

ctx._source is just a hashmap of strings, so it depends on the string representation of the date.

for epochs, you could do something like Instant.ofEpochMilli(ctx._source.revision_date_epochm).plus(Duration.ofDays(1)) - on top of my head and untested...

for epoch seconds there is Instant.ofEpochSecond(), for a string based date you would need to use a dateformatter or hope that something like ZonedDate.parse works out of the box.

Hi and thx for answer, will explore your suggestions.
\Johan

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