简体   繁体   中英

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?

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

Document in elasticsearch are immutable object. Updating a document is always a reindexing and it consist of the following steps:

  • Retrieve the JSON (that you want to reindex)
  • Change it
  • Delete the old document
  • Index a new document

Elasticsearch documentation

For the answer you can check the documentation :

In addition to being able to index and replace documents, we can also update documents. Note though that Elasticsearch does not actually do in-place updates under the hood. Whenever we do an update, Elasticsearch deletes the old document and then indexes a new document with the update applied to it in one shot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM