简介
- 说明: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
anddelete
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