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


(Vikas Gopal) #1

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


(Mujtaba Hussain) #2

Hey! :slight_smile:

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

{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

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
    }
}
'

(Vikas Gopal) #3

Thanks mujtabahussain this helps

Cheers
VG


(system) #4

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