# Updating dateparts of timestamp using query update and/or reindex/pipeline

**URL:** https://discuss.elastic.co/t/updating-dateparts-of-timestamp-using-query-update-and-or-reindex-pipeline/351618
**Category:** Elasticsearch
**Tags:** painless
**Created:** [January 23, 2024, 10:49am UTC](https://discuss.elastic.co/t/updating-dateparts-of-timestamp-using-query-update-and-or-reindex-pipeline/351618 "2024-01-23T10:49:11Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![petlit2049](https://avatars.discourse-cdn.com/v4/letter/p/e5b9ba/32.png) [@petlit2049](https://discuss.elastic.co/u/petlit2049)
#### Post date: [January 23, 2024, 10:49am UTC](https://discuss.elastic.co/t/updating-dateparts-of-timestamp-using-query-update-and-or-reindex-pipeline/351618/1 "2024-01-23T10:49:11Z")

</div>

I have log-data from various sources that have been pre-indexed into "master indices". I'd like to re-use that data by copying/re-indexing it into new indices _but_ with parts of the timestamp updated - year, month, day to be specific - we, need the time part to remain equal - only the year/month/day should be updated to the current (re-indexing) date. Can this be accomplished using painless and/or re-indexing pipeline?

---

<div class="post-metadata">

### Author: ![petlit2049](https://avatars.discourse-cdn.com/v4/letter/p/e5b9ba/32.png) [@petlit2049](https://discuss.elastic.co/u/petlit2049)
#### Post date: [January 23, 2024, 2:41pm UTC](https://discuss.elastic.co/t/updating-dateparts-of-timestamp-using-query-update-and-or-reindex-pipeline/351618/2 "2024-01-23T14:41:08Z")

</div>

I've tried (so far) something in the line of this (incomplete). I'm far from a painless guru so please bare with me:

```auto
POST _reindex
{
  "source": {
    "index": "packetbeat-*"
  },
  "dest": {
    "index": "<new index>"
  },
  "script": {
    "lang": "painless",
    "source": "def sf = new SimpleDateFormat(\"yyyy-MM-dd'T'HH:mm:ss\"); def dt = sf.parse(ctx._source['@timestamp']); def calendar = sf.getCalendar(); calendar.setTime(dt); def instant = calendar.toInstant(); def localDateTime = LocalDateTime.ofInstant(instant, ZoneOffset.UTC); ctx._source['@timestamp'] = localDateTime.plusDays(...);"
  }
}

```

The "magic" needs to happen at the end of the painless script. I need _all_ timestamps to be calculated relative to today's date. Assuming today's date is 2024-01-23:

2023-12-09 09:53:12 -\> 2024-01-22 09:53:12  
2023-12-08 12:53:12 -\> 2024-01-23 12:53:12

and so forth.

---

<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: [February 20, 2024, 2:41pm UTC](https://discuss.elastic.co/t/updating-dateparts-of-timestamp-using-query-update-and-or-reindex-pipeline/351618/3 "2024-02-20T14:41:21Z")

</div>

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