PUT /my_index/_doc/1?version=3 { "field": "new value" }
Elasticsearch uses optimistic concurrency control. If you specify the current version (3) when updating, the update succeeds and the version increments to 4.
PUT /my_index/_doc/1?version=3 { "field": "another value" }
When you specify a version lower than the current document version, Elasticsearch rejects the update with a version conflict error to avoid overwriting newer data.
With external versioning, you supply the version number. Elasticsearch updates the document only if the provided version is greater than the current version, ensuring your external system controls version order.
PUT /my_index/_doc/1?version=2 { "field": "update A" } PUT /my_index/_doc/1?version=2 { "field": "update B" }
The first update with version 2 succeeds and increments the version to 3. The second update still uses version 2, which is now outdated, so it fails with a version conflict error.
Versioning helps Elasticsearch avoid lost updates by making sure that only updates with the correct or newer version overwrite the document, preventing older data from overwriting newer changes.