J.Done
J.Done

Reputation: 3033

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?

Upvotes: 2

Views: 11975

Answers (3)

alpert
alpert

Reputation: 4655

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.

Upvotes: 1

Desmond Yang
Desmond Yang

Reputation: 61

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

Upvotes: 6

mel
mel

Reputation: 2790

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

Upvotes: 4

Related Questions