# Increment datetime using update by query

**URL:** <https://discuss.elastic.co/t/increment-datetime-using-update-by-query/181419>\
**Category:** Elasticsearch\
**Created:** [May 16, 2019, 2:01pm UTC](https://discuss.elastic.co/t/increment-datetime-using-update-by-query/181419 "2019-05-16T14:01:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![phager](https://avatars.discourse-cdn.com/v4/letter/p/ba8739/32.png) [@phager](https://discuss.elastic.co/u/phager)\
**Post date:** [May 16, 2019, 2:01pm UTC](https://discuss.elastic.co/t/increment-datetime-using-update-by-query/181419/1 "2019-05-16T14:01:10Z")

</div>

I am tasked with updating the datetime field to keep log records within a week of the current date. The data set contains records from a 1 month period and I need to increment each record by a set number of days so simply replacing the field with a specific date is not ideal. Is there a way to use update by query (example below is what I used to replace a username across all documents) to increment the datetime field?

```
POST /*/log/_update_by_query
{
  "script": {
    "inline": "ctx._source.user = 'bsmith'"
  },
  "query": {
    "term": {
      "user": "jdoe"
    }
  }
}

```

i.e. datetime: May 5th 2019, 05:35:18.753 incremented to May 16th 2019, 05:35:18.753

---

<div class="post-metadata">

**Author:** ![whatgeorgemade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/whatgeorgemade/32/103246_2.png) [@whatgeorgemade](https://discuss.elastic.co/u/whatgeorgemade)\
**Post date:** [May 16, 2019, 4:56pm UTC](https://discuss.elastic.co/t/increment-datetime-using-update-by-query/181419/2 "2019-05-16T16:56:59Z")

</div>

This is hard in Painless because the date field will be returned from the `ctx._source` context as a string. You'll need to parse it into a `LocalDateTime` type, which is messy. Once you have that, you can call the `plusDays` method to add any number of days you like, then probably call `toString` to get the string representation back and set it in the context.

This might give you enough to go on. I've already worn myself out looking through Painless and Java docs!

---

<div class="post-metadata">

**Author:** ![phager](https://avatars.discourse-cdn.com/v4/letter/p/ba8739/32.png) [@phager](https://discuss.elastic.co/u/phager)\
**Post date:** [May 16, 2019, 5:06pm UTC](https://discuss.elastic.co/t/increment-datetime-using-update-by-query/181419/3 "2019-05-16T17:06:44Z")

</div>

A colleague was able to put the following together and has resolved the issue

```
POST localhost:9200/*/_update_by_query
 
{
  "script": {
    "source": "def df = DateTimeFormatter.ofPattern('yyyy-MM-dd\\'T\\'HH:mm:ss.SSS\\'Z\\'');def tmp = LocalDateTime.parse(ctx._source.datetime,df);ctx._source.datetime=tmp.plusMonths(1);",
    "lang": "painless"
  },
  "query": {"match_all":{}}
}
```

---

<div class="post-metadata">

**Author:** ![whatgeorgemade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/whatgeorgemade/32/103246_2.png) [@whatgeorgemade](https://discuss.elastic.co/u/whatgeorgemade)\
**Post date:** [May 16, 2019, 5:13pm UTC](https://discuss.elastic.co/t/increment-datetime-using-update-by-query/181419/4 "2019-05-16T17:13:41Z")

</div>

Buy that colleague a drink! Kudos! Glad you got it resolved.

---

<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:** [June 13, 2019, 5:21pm UTC](https://discuss.elastic.co/t/increment-datetime-using-update-by-query/181419/5 "2019-06-13T17:21:29Z")

</div>

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