Elasticsearch6.0.0官方參考指南翻譯~Document APIs~Bulk API

原文:https://www.elastic.co/guide/en/elasticsearch/reference/6.0/docs-bulk.html

Bulk API

bulk API能在單個API調用中執行多個index/delete操作,這可以大大提高索引速度.

bulk請求的客戶端支持

某些官方客戶端提供了輔助工具來處理bulk請求,它會將一個索引中的文檔重新索引到另一索引:

Perl

參考Search::Elasticsearch::Client::5_0::Bulk和Search::Elasticsearch::Client::5_0::Scroll

Python

參考elasticsearch.helpers.*

REST API端點是/_bulk,它期望的輸入格式是像下面一樣使用換行符(\n)分隔的JSON(NDJSON)結構:

action_and_meta_data\n
optional_source\n
action_and_meta_data\n
optional_source\n
....
action_and_meta_data\n
optional_source\n

注意

數據的最後一行必須以換行符\n結束. 每個換行符前面可以存在一個回車符(\r). 在向端點發送請求時,Content-Type 頭必須設為application/x-ndjson.

可執行的操作包括index, create, delete 和 update.

  • index和create需要下一行(playload行)存在一個source,其語義標準index API中的op_type參數相同(即,如果已經存在具有相同索引和類型的文檔,則create會失敗,而index則會根據需要添加或替換文檔).
  • delete不需要在下一行(playload行)中提供source,其語義與標準delete API相同.
  • update則需要在下一行(playload行)中指定部分文檔,upsert和腳本及其選項.

如果要為curl提供文本文件輸入,則必須使用--data-binary標誌而非plain -d,後者不保留換行符。

例如:

$ cat requests
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
$ curl -s -H "Content-Type: application/x-ndjson" -XPOST localhost:9200/_bulk --data-binary "@requests"; echo
{"took":7, "errors": false, "items":[{"index":{"_index":"test","_type":"type1","_id":"1","_version":1,"result":"created","forced_refresh":false}}]}

由於該格式會使用\n作為分隔符,因此不要對JSON操作和source進行美化(譯者注:美化會導致錯誤的換行).

下面是正確的bulk命令序列:

POST _bulk
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }

{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }

bulk 操作的結果為:

{
"took": 30,
"errors": false,
"items": [
{
"index": {
"_index": "test",
"_type": "type1",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"status": 201,
"_seq_no" : 0,
"_primary_term": 1
}
},
{
"delete": {
"_index": "test",
"_type": "type1",
"_id": "2",
"_version": 1,
"result": "not_found",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"status": 404,
"_seq_no" : 1,
"_primary_term" : 2
}
},
{
"create": {
"_index": "test",
"_type": "type1",
"_id": "3",

"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"status": 201,
"_seq_no" : 2,
"_primary_term" : 3
}
},
{
"update": {
"_index": "test",
"_type": "type1",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"status": 200,
"_seq_no" : 3,
"_primary_term" : 4
}
}
]
}

端點可以是/_bulk, /{index}/_bulk, {index}/{type}/_bulk. 當端點包含index或index/type時,默認情況下它會對那些未顯示提示這些值(指的是index/type)的bulk項使用這些值(即:端點中的index/type).

關於格式的說明

其想法是儘可能快地處理這些bulk請求,但由於某些操作會將請求重定向到其他節點的其他分片上,因此在接收節方只會解析action_meta_data.

使用該協議的客戶端庫應該儘可能地在客戶端執行類似操作,並儘可能地減少緩衝(buffering).

bulk操作的響應是一個大的JSON結構,它包含了每個操作的執行結果. 單個操作失敗不會影響其它的操作.

單個bulk調用中,對於可執行多少操作的數量並沒有一個準確值.您應該嘗試不同的設置,以便找到最佳的工作負載大小.

如果使用HTTP API,請確保客戶端不會發送HTTP塊(chunks),因為這會減慢速度。

Versioning

每個bulk項都可通過_version/version字段來包含一個版本值.

它會根據_version映射來自動跟蹤index /delete操作的行為.

此外,它還支持version_type/_version_type (參考versioning(https://www.elastic.co/guide/en/elasticsearch/reference/6.0/docs-index_.html#index-versioning))

Routing

每個bulk項都可通過_routing/routing字段來包含路由值.

它會基於_routing映射來自動跟蹤index /delete操作的行為。

Wait For Active Shards

在執行bulk調用時,您可以設置wait_for_active_shards參數來要求處理bulk請求前必須存在活躍分片副本的最小數量(譯者注:達到最小數量的分片副本時才處理bulk請求).

有關詳情及其使用示例,請參考此處(https://www.elastic.co/guide/en/elasticsearch/reference/6.0/docs-index_.html#index-wait-for-active-shards)。

Refresh

控制該請求作出的修改何時對搜索可見.

參考refresh(https://www.elastic.co/guide/en/elasticsearch/reference/6.0/docs-refresh.html).

Update

當執行update操作時,可在操作中設置_retry_on_conflict參數(不是payload行),以便在碰到版本衝突時用於指定update操作可以重試的次數.

update操作的payload支持如下選項:

doc (可以是部分文檔), upsert, doc_as_upsert,>

參考update documentation來了解選項的詳細說明.

下面是update操作示例:

POST _bulk

#譯者注:這是action行

{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1", "_retry_on_conflict" : 3} }

#譯者注:這是payload行

{ "doc" : {"field" : "value"} }

{ "update" : { "_id" : "0", "_type" : "type1", "_index" : "index1", "_retry_on_conflict" : 3} }

{ "script" : { "source": "ctx._source.counter += params.param1", "lang" : "painless", "params" : {"param1" : 1}}, "upsert" : {"counter" : 1}}

{ "update" : {"_id" : "2", "_type" : "type1", "_index" : "index1", "_retry_on_conflict" : 3} }

{ "doc" : {"field" : "value"}, "doc_as_upsert" : true }

{ "update" : {"_id" : "3", "_type" : "type1", "_index" : "index1", "_source" : true} }

{ "doc" : {"field" : "value"} }

{ "update" : {"_id" : "4", "_type" : "type1", "_index" : "index1"} }

{ "doc" : {"field" : "value"}, "_source": true}

Security

參考URL-based access control(https://www.elastic.co/guide/en/elasticsearch/reference/6.0/url-access-control.html)


分享到:


相關文章: