# ELASTICSEARCH - Update the value of a field to current date

**URL:** <https://discuss.elastic.co/t/elasticsearch-update-the-value-of-a-field-to-current-date/308931>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [July 5, 2022, 3:10pm UTC](https://discuss.elastic.co/t/elasticsearch-update-the-value-of-a-field-to-current-date/308931 "2022-07-05T15:10:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miguel\_BA](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/miguel_ba/32/46926_2.png) [@Miguel\_BA](https://discuss.elastic.co/u/Miguel_BA)\
**Post date:** [July 5, 2022, 3:10pm UTC](https://discuss.elastic.co/t/elasticsearch-update-the-value-of-a-field-to-current-date/308931/1 "2022-07-05T15:10:10Z")

</div>

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:

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

```

I'm doing it through the following way:

```auto
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:

```auto
"timestamp" : {
          "type" : "date"
        }

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

```

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [July 5, 2022, 3:39pm UTC](https://discuss.elastic.co/t/elasticsearch-update-the-value-of-a-field-to-current-date/308931/2 "2022-07-05T15:39:37Z")

</div>

As [the doc](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-datetime.html#_datetime_now) says, `now` is not supported in most Painless contexts.

To obtain current date, use [ingest pipline](https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest.html#access-ingest-metadata) 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.

---

<div class="post-metadata">

**Author:** ![Miguel\_BA](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/miguel_ba/32/46926_2.png) [@Miguel\_BA](https://discuss.elastic.co/u/Miguel_BA)\
**Post date:** [July 6, 2022, 10:34am UTC](https://discuss.elastic.co/t/elasticsearch-update-the-value-of-a-field-to-current-date/308931/3 "2022-07-06T10:34:46Z")

</div>

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

```auto
[
  {
    "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?

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [July 6, 2022, 11:06am UTC](https://discuss.elastic.co/t/elasticsearch-update-the-value-of-a-field-to-current-date/308931/4 "2022-07-06T11:06:53Z")

</div>

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

> <https://stackoverflow.com/questions/68286853/elasticsearch-ingest-pipeline-create-and-update-timestamp-field/68298454#68298454>

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 3, 2022, 11:07am UTC](https://discuss.elastic.co/t/elasticsearch-update-the-value-of-a-field-to-current-date/308931/5 "2022-08-03T11:07:33Z")

</div>

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