Documents in
Elastic search are immutable, I mean we can’t change them; we need to update
them entirely.
How to update them?
Update the
document using PUT request.
GET
xyz/employees/1
Above
snippet return following information.
{ "_index": "xyz", "_type": "employees", "_id": "1", "_version": 5, "found": true, "_source": { "firstName": "Phalgun", "lastName": "Garimella", "hobbies": [ "Watching movies", "Stamp collection", "Reading books", "Playing Cricket" ], "age": 30 } }
Now update
the document using PUT request.
PUT xyz/employees/1 { "firstName" : "Sunil Kumar", "lastName" : "Kumar", "age" : 36 }
You will get
following response.
{ "_index": "xyz", "_type": "employees", "_id": "1", "_version": 6, "created": false }
As you
observe version number is incremented by 1 and created attribute is set to
false, since we are not creating new document, we just updated existing
document.
GET xyz/employees/1 { "_index": "xyz", "_type": "employees", "_id": "1", "_version": 6, "found": true, "_source": { "firstName": "Sunil Kumar", "lastName": "Kumar", "age": 36 } }
As you
observe the response, it don't contains hobbies field, it is because we are
overriding entire document with new data (Since documents in elastic search are
immutable).
What happen to old document?
Elastic
search mark old version document as deleted and update it with new version
document. Old document is not deleted immediately; it will be deleted by
background process after some time.
No comments:
Post a Comment