# Update by query date field (painless) and add hours

**URL:** <https://discuss.elastic.co/t/update-by-query-date-field-painless-and-add-hours/252291>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [October 16, 2020, 3:55am UTC](https://discuss.elastic.co/t/update-by-query-date-field-painless-and-add-hours/252291 "2020-10-16T03:55:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [October 16, 2020, 3:55am UTC](https://discuss.elastic.co/t/update-by-query-date-field-painless-and-add-hours/252291/1 "2020-10-16T03:55:27Z")

</div>

Hi,  
Some basics in painless (the painful syntax)  
Trying to update a filed to have the timestamp value + X hours.

I was trying the following

```auto
POST index*/_update_by_query
{
  "script": {
    "source": "ctx._source.timestamp_shifted = ctx._source.timestamp + 39600",
    "lang": "painless"
  },
  "query": {
          "term": {
                  "msg_source_type_name.keyword": "xxx"
          }
  }
}

```

```auto
POST index*/_update_by_query
{
  "script": {
    "source": "ctx._source.timestamp_shifted = ctx._source.timestamp.plusHours(11)",
    "lang": "painless"
  },
  "query": {
          "term": {
                  "msg_source_type_name.keyword": "xxx"
          }
  }
}

```

Anyone can point me to the right syntax?

Cheers!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 16, 2020, 2:06pm UTC](https://discuss.elastic.co/t/update-by-query-date-field-painless-and-add-hours/252291/2 "2020-10-16T14:06:59Z")

</div>

Hey,

if you access the `_source` the field of your date will be a simple string, so you need to parse it as a date first, i.e. using `ZonedDateTime.parse()`, and then you can call that `.plusHours()` method.

Hope that helps as a start.

--Alex

---

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [October 19, 2020, 2:49am UTC](https://discuss.elastic.co/t/update-by-query-date-field-painless-and-add-hours/252291/3 "2020-10-19T02:49:21Z")

</div>

@spinscale  
Thanks for that.  
Have an example? Or documentation?  
Having syntax issues again.  
Cheers!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 19, 2020, 11:15am UTC](https://discuss.elastic.co/t/update-by-query-date-field-painless-and-add-hours/252291/4 "2020-10-19T11:15:31Z")

</div>

There is a snippet of local date time parsing in the [examples repo]([https://github.com/elastic/examples/blob/master/Alerting/Sample](https://github.com/elastic/examples/blob/master/Alerting/Sample) Watches/lateral\_movement\_in\_user\_comm/scripts/lower\_time.json) that could help you.

---

<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:** [November 16, 2020, 11:15am UTC](https://discuss.elastic.co/t/update-by-query-date-field-painless-and-add-hours/252291/5 "2020-11-16T11:15:33Z")

</div>

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