Elastic - prevent updating documents Elastic - prevent updating documents elasticsearch elasticsearch

Elastic - prevent updating documents


If you use index API with a specific id, elasticsearch will update that document with the newer document. But if you use create APIwith specific id, you allow elasticsearch for "put-if-absent" behavior. it means, by using create, the document index operation will fail if a document by that id already exists in the index.This is how you can use create in elasticsearch lower 7:

POST index001/_doc/1?op_type=create'{  "stuff": {        }}'

and this is using for elasticsearch 7:

POST index001/_create/1'{  "stuff": {        }}'


By default, elasticsearch documents are immutable as they are stored in segments which are immutable, and any update to an existing document means(creating a new document and marking old documents as deleted). But this doesn't make them immutable externally.

You can simply use the GET API based on document id before sending the update request or if you want to avoid the extra API call, use an optimistic locking doc of ES and implement accordingly to avoid further update to your documents.