天天看點

es 之 批量處理 bulk api

簡介

  • 說明:The bulk API makes it possible to perform many index/delete operations in a single API call. This can greatly increase the indexing speed。
  • 使用以下結構
action_and_meta_data\n
optional_source\n
action_and_meta_data\n
optional_source\n
....
action_and_meta_data\n
optional_source\n
           

示例說明

  • 請求頭

    Content-Type: application/x-ndjson

  • 請求方法:

    index

    ,

    create

    ,

    delete

    and

    update

    • index ,create 下一行需要資源。(已存在的情況下create 會失敗,index 則會添加或者替換已有的文檔);
    • delete 下一行不需要資源;
    • update 需要部分文檔,upsert and script and its options 需要在下一行指明。

示例:

POST _bulk
{ "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "_doc", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "_doc", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "_doc", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
           

傳回結果大概:

{
   "took": 30,
   "errors": false,
   "items"[
   {},
   ]
}
           

說明:

  • update 更新字段 field2 的值為 value2
  • 操作中的屬性 _id, _type, _index 順序無影響
  • url 的 結尾可以是 /_bulk, /{index}/_bulk, and {index}/{type}/_bulk;如果在 URL 中指定之後在 payload line 中指定會将 url 中的 index 或 type 覆寫

文檔來源

https://www.elastic.co/guide/en/elasticsearch/reference/6.2/docs-bulk.html

繼續閱讀