This article translated from: www.elastic.co/guide/en/el…
For Elasticsearch, you need to modify, update, and delete data.
Modify the data
Elasticsearch provides data manipulation and search in near real time. By default, you can see your index/update/delete operation data appear in search results after a one-second delay (refresh interval). This is an important difference from other platforms, such as SQL, where this type of platform data is available immediately after a transaction has completed.
Index and replace documents
We’ve seen before how we index a document. As follows:
PUT /customer/external/1? pretty {"name": "John Doe"
}Copy the code
Next, index the specified document above to the custom index of the external type, ID 1. If we execute the above command again with a different (or the same) document, Elasticsearch will replace (i.e. re-index) the existing document with ID 1 and appear at the top:
PUT /customer/external/1? pretty {"name": "Jane Doe"
}Copy the code
This changes the name of the document with ID 1 from “John Doe” to “Jane Doe”. On the other hand, if we use a different ID, the new document will be indexed and the existing document already in the index will remain unchanged (equivalent to a copy) as follows:
PUT /customer/external/2? pretty {"name": "Jane Doe"
}Copy the code
The above index is a new document with ID 2. When indexing, the ID part is optional. If not specified, Elasticsearch generates a random ID and then uses it to index the document. The return value of the index API call contains the actual ID generated by Elasticsearch (or whatever we explicitly specified in the previous example).
This example shows how to index a document without an explicit ID:
POST /customer/external? pretty {"name": "Jane Doe"
}Copy the code
Notice that in the above case, we used the POST verb instead of PUT because we didn’t specify an ID.
Update the document
In addition to being able to index and replace documents, we can also update them. Note that Elasticsearch is not actually updated at the bottom. Elasticsearch will delete the old document every time we update it, then index (copy) a new document and update the application once.
This example shows how to update our previous document (ID 1) by changing the name field to “Jane Doe” :
POST /customer/external/1/_update? pretty {"doc": { "name": "Jane Doe"}}Copy the code
This example shows how to update our previous document (ID 1) by changing the name field to “Jane Doe” while adding an age field to it:
POST /customer/external/1/_update? pretty {"doc": { "name": "Jane Doe"."age"20}} :Copy the code
You can also use simple scripts to perform updates. This example uses a script to increase the age by 5:
POST /customer/external/1/_update? pretty {"script" : "ctx._source.age += 5"
}Copy the code
In the above example, ctx._source means that the current source document is about to be updated.
Note that at the time of this writing, updates can only be performed on a single document at a time. In the future, Elasticsearch may provide the ability to UPDATE multiple documents (such as SQL update-WHERE statements) with query criteria.
Delete the document
Deleting documents is fairly simple. This example shows how to delete our previous customer with ID 2:
DELETE /customer/external/2? prettyCopy the code
See the _delete_by_query API to delete all documents that match a particular query. It is worth noting that it is more efficient to delete the entire index, rather than using the “delete by query” API to delete compliant documents.
Batch processing
In addition to being able to index, update, and delete individual documents, Elasticsearch provides the ability to perform any of these operations in bulk using the _BULK API. This feature is important because it provides a very efficient mechanism for doing multiple operations as quickly as possible with as few network round-trips as possible.
As a quick example, the following call specifies two documents (ID 1-John Doe and ID 2-Jane Doe) in a batch operation:
POST /customer/external/_bulk? pretty {"index": {"_id":"1"}}
{"name": "John Doe" }
{"index": {"_id":"2"}}
{"name": "Jane Doe" }Copy the code
This example updates the first document (ID 1) and then deletes the second document (ID 2) in a batch operation:
POST /customer/external/_bulk? pretty {"update": {"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe"}} {"delete": {"_id":"2"}}Copy the code
Note that for the delete operation, there is no corresponding source document after it, because deletion requires only the ID of the document to be deleted.
The Bulk API does not fail because one of the operations fails. If a single action fails for any reason, it continues to process the remaining actions that follow. When the batch API returns, it provides a state for each operation (in the same order it was sent) so that · a particular operation can be checked for failure.