# Reindex data with a new Field

**URL:** https://discuss.elastic.co/t/reindex-data-with-a-new-field/115073
**Category:** Elasticsearch
**Created:** [January 11, 2018, 11:42am UTC](https://discuss.elastic.co/t/reindex-data-with-a-new-field/115073 "2018-01-11T11:42:38Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mikelD](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@mikelD](https://discuss.elastic.co/u/mikelD)
#### Post date: [January 11, 2018, 11:42am UTC](https://discuss.elastic.co/t/reindex-data-with-a-new-field/115073/1 "2018-01-11T11:42:38Z")

</div>

Hi,

Is there anyway to reindex the data and create a new field, from the data in the documents. For example, reindex an entire index and create a new field Hour(number) from a date type field(YYYY-mm-dd, HH-mm-ss). I Know that Lucene has an expresion for this(doc['@timestamp'].date.getHourOfDay()), but can this be used to create a new field when you reindex?

I need this new field for a search query and the scripted fields can't be used for this, the alternative is using a script on the query but it can be bad for the search time if the number of documents searched continue to increase.

---

<div class="post-metadata">

### Author: ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)
#### Post date: [January 19, 2018, 3:43pm UTC](https://discuss.elastic.co/t/reindex-data-with-a-new-field/115073/2 "2018-01-19T15:43:48Z")

</div>

There is an example of using reindex to modify the document in the docs [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html#docs-reindex-change-name). It changes the name of a field but you can use the same `script` construct to do what you want to do. I suspect the script looks something like `"script": "ctx._source['timestamp_hour'] = ctx._source['@timestamp'].getHourOfDay()`. You are right that it will be much better at search time to use this new field. Watch the time zone, btw. I believe the hour that you get in this case is UTC.

You could also do this with `_update_by_query` because it just introduces a new field which is a change you can do on an existing index. You'll end up with deleted documents in your index but merge should remove them in time. Not all of them, but a bunch. That might be easier to deal with depending on what you are doing.

---

<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 16, 2018, 3:43pm UTC](https://discuss.elastic.co/t/reindex-data-with-a-new-field/115073/3 "2018-02-16T15:43:58Z")

</div>

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