# Add new field to existing documents in an index

**URL:** <https://discuss.elastic.co/t/add-new-field-to-existing-documents-in-an-index/171186>\
**Category:** Elasticsearch\
**Created:** [March 6, 2019, 7:04pm UTC](https://discuss.elastic.co/t/add-new-field-to-existing-documents-in-an-index/171186 "2019-03-06T19:04:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aniaks](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@aniaks](https://discuss.elastic.co/u/aniaks)\
**Post date:** [March 6, 2019, 7:04pm UTC](https://discuss.elastic.co/t/add-new-field-to-existing-documents-in-an-index/171186/1 "2019-03-06T19:04:31Z")

</div>

Hi,

I need to add a new field to all the docs present in the index. Is there a way to accomplish this ?

---

<div class="post-metadata">

**Author:** ![glenacota](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/glenacota/32/41367_2.png) [@glenacota](https://discuss.elastic.co/u/glenacota)\
**Post date:** [March 6, 2019, 7:39pm UTC](https://discuss.elastic.co/t/add-new-field-to-existing-documents-in-an-index/171186/2 "2019-03-06T19:39:14Z")

</div>

Hi,  
if all you have to do is to add a new field, you can accomplish it with an `update_by_query` (see [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/6.6/docs-update-by-query.html)) and a simple script. Something like

```auto
POST your_index/_update_by_query
{
  "script": {
    "source": "ctx._source.new_field = null"
  }
}

```

Of course, feel free to choose any other null value.

---

<div class="post-metadata">

**Author:** ![aniaks](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@aniaks](https://discuss.elastic.co/u/aniaks)\
**Post date:** [March 7, 2019, 2:37pm UTC](https://discuss.elastic.co/t/add-new-field-to-existing-documents-in-an-index/171186/3 "2019-03-07T14:37:28Z")

</div>

yes it worked .....

---

<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:** [April 4, 2019, 2:37pm UTC](https://discuss.elastic.co/t/add-new-field-to-existing-documents-in-an-index/171186/4 "2019-04-04T14:37:35Z")

</div>

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