# Add field to existing document by query?

**URL:** <https://discuss.elastic.co/t/add-field-to-existing-document-by-query/68874>\
**Category:** Elasticsearch\
**Created:** [December 13, 2016, 1:48pm UTC](https://discuss.elastic.co/t/add-field-to-existing-document-by-query/68874 "2016-12-13T13:48:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![german23](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/german23/32/11052_2.png) [@german23](https://discuss.elastic.co/u/german23)\
**Post date:** [December 13, 2016, 1:48pm UTC](https://discuss.elastic.co/t/add-field-to-existing-document-by-query/68874/1 "2016-12-13T13:48:40Z")

</div>

Hi folks,

I want to add a new field to multiple existing documents.

e.g.

All documents that have the value "93.22.14.132" in the field "ip" should get the field "status" with the value "okay".

Is there a built-in api of ES that allows me to do that or do i need to fetch all IDs of the documents with the matching ip first and than update it one-by-one?

Any feedback is appreciated

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [December 13, 2016, 2:14pm UTC](https://discuss.elastic.co/t/add-field-to-existing-document-by-query/68874/2 "2016-12-13T14:14:55Z")

</div>

Hi,

you can use the [Update By Query Api](https://www.elastic.co/guide/en/elasticsearch/reference/5.1/docs-update-by-query.html#picking-up-a-new-property) for this that is available since version 2.3. In your example, it would look something like:

```auto
POST index/_update_by_query
{
  "script": {
    "inline": "ctx._source.status = \"okay\"",
    "lang": "painless"
  },
  "query": {
    "match": {
      "ip": "93.22.14.132"
    }
  }
}

```

but this might vary depending on your version of elasticsearch.

---

<div class="post-metadata">

**Author:** ![german23](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/german23/32/11052_2.png) [@german23](https://discuss.elastic.co/u/german23)\
**Post date:** [December 13, 2016, 3:14pm UTC](https://discuss.elastic.co/t/add-field-to-existing-document-by-query/68874/3 "2016-12-13T15:14:58Z")

</div>

I am using ES2.4 currently,

After enabling the inline\_scripting in the elasticsearch config (script.inline: true ) and removing the "lang" parameter (painless is 5.X only as far as i know), this command did the exact thing i wanted to do.

Thank you very much!

---

<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:** [December 13, 2016, 3:32pm UTC](https://discuss.elastic.co/t/add-field-to-existing-document-by-query/68874/4 "2016-12-13T15:32:21Z")

</div>

> [@german23](#):
>
> After enabling the inline\_scripting in the elasticsearch config (script.inline: true ) and removing the "lang" parameter (painless is 5.X only as far as i know), this command did the exact thing i wanted to do.

In 5.x you shouldn't have to enabled inline scripting for painless. It is enabled by default.

You do have the option of using `"lang": "groovy"` and for that you'd have to enable inline scripting.

---

<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:** [January 10, 2017, 3:32pm UTC](https://discuss.elastic.co/t/add-field-to-existing-document-by-query/68874/5 "2017-01-10T15:32:22Z")

</div>

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