# How to update and add new filed in the existing Document?

**URL:** <https://discuss.elastic.co/t/how-to-update-and-add-new-filed-in-the-existing-document/102368>\
**Category:** Elasticsearch\
**Created:** [October 1, 2017, 9:35am UTC](https://discuss.elastic.co/t/how-to-update-and-add-new-filed-in-the-existing-document/102368 "2017-10-01T09:35:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikas\_gopal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vikas_gopal/32/47661_2.png) [@vikas\_gopal](https://discuss.elastic.co/u/vikas_gopal)\
**Post date:** [October 1, 2017, 9:35am UTC](https://discuss.elastic.co/t/how-to-update-and-add-new-filed-in-the-existing-document/102368/1 "2017-10-01T09:35:48Z")

</div>

Hi Experts,

I want to update a specific field value in a document . Also I want to add new filed in this document . So here is the document that I want to update

GET \_search  
{  
"query": {  
"query\_string": {  
"query": "created:no AND eventId:95392012643",  
"analyze\_wildcard": true  
}  
}  
}

I want to change created: no to created:yes also want to add new filed like ticket\_number :INC0001231

Can someone help me to achieve this . I was checking the documents and found this can be achieved with update API , but unfortunately not able to achieve it .

Please help

Regards  
VG

---

<div class="post-metadata">

**Author:** ![mujtabahussain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mujtabahussain/32/17514_2.png) [@mujtabahussain](https://discuss.elastic.co/u/mujtabahussain)\
**Post date:** [October 3, 2017, 1:00am UTC](https://discuss.elastic.co/t/how-to-update-and-add-new-filed-in-the-existing-document/102368/2 "2017-10-03T01:00:20Z")

</div>

Hey! 🙂

OK, first things first, you have a document in an index that you want to update!

If you know the ID of the document, you can use the update API and specify the id, and use

```json
{doc: data, upsert: data }

```

Instead of `upsert` param in the `body` block, you can also use `script`

block to update the document. Here `data` would contain the new field as well provided the index mapping supports it.

API example taken from [update API docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html)

```bash
curl -XPOST 'localhost:9200/test/type1/1/_update?pretty' -H 'Content-Type: application/json' -d'
{
    "script" : {
        "source": "ctx._source.counter += params.count",
        "lang": "painless",
        "params" : {
            "count" : 4
        }
    },
    "upsert" : {
        "counter" : 1
    }
}
'

```

---

<div class="post-metadata">

**Author:** ![vikas\_gopal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vikas_gopal/32/47661_2.png) [@vikas\_gopal](https://discuss.elastic.co/u/vikas_gopal)\
**Post date:** [October 3, 2017, 8:49am UTC](https://discuss.elastic.co/t/how-to-update-and-add-new-filed-in-the-existing-document/102368/3 "2017-10-03T08:49:06Z")

</div>

> [@vikas\_gopal](#):
>
> GET \_search
> 
> {
> 
> "query": {
> 
> "query\_string": {
> 
> "query": "created:no AND eventId:95392012643",
> 
> "analyze\_wildcard": true
> 
> }
> 
> }
> 
> }

Thanks mujtabahussain this helps

Cheers  
VG

---

<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:** [October 31, 2017, 8:49am UTC](https://discuss.elastic.co/t/how-to-update-and-add-new-filed-in-the-existing-document/102368/4 "2017-10-31T08:49:06Z")

</div>

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