Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch - How to update document

How does elasticsearch update document? It will delete original document and make new one? I've heard this is how nosql's updating method. does elasticsearch do, same as any other nosql db? or It will replace/insert just field which need to be?

like image 665
J.Done Avatar asked Jan 20 '26 17:01

J.Done


1 Answers

For example, I'm running with Elasticsearh 7.0.0. First, I created one document,

PUT /employee/_doc/1

{
    "first_name" : "John",
    "last_name" : "Snow",
    "age" : 19,
    "about" : "King in the north",
    "sex" : "male"
}

Then I updated it via

POST /employee/_update/1/

{
    "doc": {  
        "first_name" : "Aegon",
        "last_name" : "Targaryen",
        "skill": "fighting and leading"
    }
}

Finally, I got correct result when

GET /employee/_doc/1

{
    "_index" : "employee",
    "_type" : "_doc",
    "_id" : "1",
    "_version" : 9,
    "_seq_no" : 11,
    "_primary_term" : 1,
    "found" : true,
    "_source" : {
        "first_name" : "Aegon",
        "last_name" : "Targaryen",
        "age" : 19,
        "about" : "King in the north",
        "sex" : "male",
        "skill" : "fighting and leading"
    }
}
like image 129
Desmond Yang Avatar answered Jan 23 '26 15:01

Desmond Yang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!