Problem:
Need to replace existing documents. Need to update only part of the document with / without scripting. Need to do upserts.
Solution Summary:
We can replace elastic search documents using a PUT request with a request body.
We can update only part of a document using a POST request with _update endpoint. ES documents are immutable. So internally _update will get the document, update the fields and PUT again.
We can use scrpting to update based on existing field values.
Finally, we can use upserts to update only if the document exist.
Prerequisites:
Solution Steps:
Initial State
We will create a new document in the employee index and use it for some of the following excercises.
PUT /employee/_doc/2?pretty
{
"name": "Sneha",
"phone": "9999999999"
}
Verify it using: GET /employee/_doc/2?pretty
Replacing Documents
We can replace a document using PUT.
PUT /employee/_doc/2?pretty
{
"name": "Sneha",
"salary": 5000000
}
Verify it using: GET /employee/_doc/2?pretty
You can see that there is no more a phone field, but a salary field.
Note:
-
Using POST will also have same behaviour.
-
When replacing a document, ES will still still update _version (verify)
Next, we can see how to update some fields alone.
Updating Documents
We can use _update endpoint and the "doc" property.
POST /employee/_doc/2/_update
{
"doc": {
"salary": 10000000
}
}
Verify it using: GET /employee/_doc/2?pretty
Note:
-
Only POST is allowed and PUT is not allowed here.
-
ES will increment _version
Updating Documents Using Script
POST /employee/_doc/2/_update
{
"script": "ctx._source.salary += 10"
}
Note: ctx variable reprecents current document.
Upsert Example
Create document if it does not exist executing the upsert part.
POST /employee/_doc/4/_update
{
"script": "ctx._source.salary +=10",
"upsert": {
"salary": 5000000
}
}
Verify by executing GET before first execution, after first execution and after second execution:
GET /employee/_doc/4/?pretty
Note: You cannot use this syntax to create a field for an existing document.
Recent comments