# What is the best practise to add a new field to an index in elasticsearch?

**URL:** https://discuss.elastic.co/t/what-is-the-best-practise-to-add-a-new-field-to-an-index-in-elasticsearch/344624
**Category:** Elasticsearch
**Created:** [October 9, 2023, 6:55am UTC](https://discuss.elastic.co/t/what-is-the-best-practise-to-add-a-new-field-to-an-index-in-elasticsearch/344624 "2023-10-09T06:55:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![stephane\_chan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephane_chan/32/122193_2.png) [@stephane\_chan](https://discuss.elastic.co/u/stephane_chan)
#### Post date: [October 9, 2023, 6:55am UTC](https://discuss.elastic.co/t/what-is-the-best-practise-to-add-a-new-field-to-an-index-in-elasticsearch/344624/1 "2023-10-09T06:55:00Z")

</div>

Hi,  
My problem is that I want to add a new field in my index but the value of the field is in the database, is it possible to update the data in my index by doing \_update\_by\_query? if it is possible, how to do this to connect to the database (postgres) ?  
or should I write a script to update each data and call the rest api each time?

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [October 9, 2023, 7:08am UTC](https://discuss.elastic.co/t/what-is-the-best-practise-to-add-a-new-field-to-an-index-in-elasticsearch/344624/2 "2023-10-09T07:08:40Z")

</div>

There's no direct way (yet) to do such a thing. So this is something you need to manage by yourself.

You might be able to do this using Logstash using some jdbc filter.

If you need to update all the documents of your index, I'd recommend reindexing instead of doing updates.

Unless you can set the value to update from the current document (like, extracting the day of the week from a date field), update by query won't help in that case.

---

<div class="post-metadata">

### Author: ![stephane\_chan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephane_chan/32/122193_2.png) [@stephane\_chan](https://discuss.elastic.co/u/stephane_chan)
#### Post date: [October 9, 2023, 7:27am UTC](https://discuss.elastic.co/t/what-is-the-best-practise-to-add-a-new-field-to-an-index-in-elasticsearch/344624/3 "2023-10-09T07:27:48Z")

</div>

And how to do the reindexing with the addition of the new field instead of updates ?

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [October 9, 2023, 8:10am UTC](https://discuss.elastic.co/t/what-is-the-best-practise-to-add-a-new-field-to-an-index-in-elasticsearch/344624/4 "2023-10-09T08:10:24Z")

</div>

The same way you indexed first your documents.

---

<div class="post-metadata">

### Author: ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)
#### Post date: [October 9, 2023, 12:25pm UTC](https://discuss.elastic.co/t/what-is-the-best-practise-to-add-a-new-field-to-an-index-in-elasticsearch/344624/5 "2023-10-09T12:25:07Z")

</div>

I have done it few time.

here is how I did using python  
read every record from index in loop using elasticsearch\_dsl, don't forget to get \_id  
create new recordset with same \_id of original  
write them back to same index and it will overwrite (delete/create) same record.

---

<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 6, 2023, 12:25pm UTC](https://discuss.elastic.co/t/what-is-the-best-practise-to-add-a-new-field-to-an-index-in-elasticsearch/344624/6 "2023-11-06T12:25:51Z")

</div>

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