ELASTICSEARCH - Update the value of a field to current date

I'm looking for a way to update the value of a field through update_by_query in the field: reviewed_data in the next document:

    {
      "reviewed_date" : "2022-07-05T09:31:04.742077702Z",
      "timestamp" : "2022-07-05T10:18:52.353852943Z"
    }
  

I'm doing it through the following way:

POST index1/_update_by_query
{
  "script": {
    "source": "ctx._source.reviewed_date = OffsetDateTime.parse(ctx._source.reviewed_date)" , 
    "lang": "painless"
  },
  "query": {
    "term": {
      "_id":  "23r2fvwc3drgr4f2323dsd"
    }
  }
}

This way I only manage to update the value of the timestamp date, even though I am indicating reviewed_date. But I would like to be able to assign the value directly to reviewed_data without modifying other fields.

Mapping:

"timestamp" : {
          "type" : "date"
        }

"reviewed_date" : {
          "type" : "date"
        },

As the doc says, now is not supported in most Painless contexts.

To obtain current date, use ingest pipline and its metadata of timestamp.

I guess the timestamp field of your index was updated not by the update query but by some ingest pipeline defined elsewhere. Your script set reviewed_date field the same original data of reviewed_date field.

Thank you!
After your message I created a pipeline with 2 fields:

[
  {
    "set": {
      "field": "reviewed_date",
      "value": "{{_ingest.timestamp}}"
    }
  },
  {
    "set": {
      "field": "timestamp",
      "value": "{{_ingest.timestamp}}"
    }
  }
]

Even so, when I make a post to update the date field, both fields are updated: timestamp and reviewed_date.
Is there a possibility to only update the reviewed_date field?

If you want to keep timestamp, this post may be related.

1 Like

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