1、簡介
主要介紹當在ES中添加了document後,如何更新、刪除、批量操作數據。
2、內容
1、更新文檔
PUT /customer/_doc/1?pretty
{
"name": "John Doe"
}
說明:更新與保存都是使用put方法。
2、刪除文檔
DELETE /customer/_doc/2?pretty
說明:可以通過 _delete_by_query API刪除匹配到指定內容的所有文檔
3、批量操作文檔
批量操作文檔時,api接口爲_bulk ,每一行數據必須以換行符\n結束。每個換行符前面可以有一個回車\r。向該端點發送請求時,應該將Content-Type頭設置爲application/x-ndjson。裏面執行的動作可以是index,create,delete,update。其中index和create的下一行中,必須要含一個源文件。update希望在下一行指定部分doc、upsert和腳本及其選項,例如
POST /customer/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
將保存兩個document,在index的下一行緊跟着文檔的內容
POST /customer/_bulk?pretty
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
第一行爲更新id爲1的document,第三行爲更新document。