WARNING: The 2.x versions of Elasticsearch have passed their EOL dates. If you are running a 2.x version, we strongly advise you to upgrade.
This documentation is no longer maintained and may be removed. For the latest information, see the current Elasticsearch documentation.
Updating a Whole Document
editUpdating a Whole Document
editDocuments in Elasticsearch are immutable; we cannot change them. Instead, if
we need to update an existing document, we reindex or replace it, which we
can do using the same index
API that we have already discussed in
Indexing a Document.
PUT /website/blog/123 { "title": "My first blog entry", "text": "I am starting to get the hang of this...", "date": "2014/01/02" }
In the response, we can see that Elasticsearch has incremented the _version
number:
The |
Internally, Elasticsearch has marked the old document as deleted and added an entirely new document. The old version of the document doesn’t disappear immediately, although you won’t be able to access it. Elasticsearch cleans up deleted documents in the background as you continue to index more data.
Later in this chapter, we introduce the update
API, which can be used to
make partial updates to a document. This API appears to
change documents in place, but actually Elasticsearch is following exactly the
same process as described previously:
- Retrieve the JSON from the old document
- Change it
- Delete the old document
- Index a new document
The only difference is that the update
API achieves this through a single
client request, instead of requiring separate get
and index
requests.