1. 程式人生 > >Elasticsearch使用bulk批量執行命令

Elasticsearch使用bulk批量執行命令

前言

使用bulk命令,批量執行,用於增刪改的操作,用於提高效率!

注意:在一個命令結束前,命令頭裡面的程式碼不能換行;

刪除、修改命令有請求頭請求體,這兩部分需要換行!

metadata 需要指明需要被操作文件的_index_type以及_id

舉例

POST /_bulk
{"delete": {"_index": "test_index","_type": "test_type","_id": "1"}}
{"create": {"_index": "test_index","_type": "test_type","_id": "4"}}
{"desc": "id is 4"}
{"index": {"_index": "test_index","_type": "test_type","_id": "2"}}
{"age": 100}
{"index": {"_index": "test_index","_type": "test_type","_id": "5"}}
{"desc": "id is 5"}
{"update": {"_index": "test_index","_type": "test_type","_id": "3"}}
{"doc":{"desc": "id is update to 3333"}} 

解析

1.刪除命令

刪除命令是隻有一行,沒有請求體:

{"delete": {"_index": "test_index","_type": "test_type","_id": "1"}}

2.增加命令

請求頭請求體需要換行!

{"create": {"_index": "test_index","_type": "test_type","_id": "4"}}

{"desc": "id is 4"}

或者使用index命令,

{"index": {"_index": "test_index","_type": "test_type","_id": "5"}}

{"desc": "id is 5"}

3.修改命令

請求頭請求體需要換行!

{"update": {"_index": "test_index","_type": "test_type","_id": "3"}}

{"doc":{"desc": "id is update to 3333"}} 

或者使用index命令

{"index": {"_index": "test_index","_type": "test_type","_id": "2"}}
{"age": 100}

OK,GAME OVER!