elasticsearch提供批量处理的api: bulkweb
bulk API 容许在单个步骤中进行屡次 create 、 index 、 update 或 delete 请求json
bulk的请求体:api
{ action: { metadata }}\n { request body }\n { action: { metadata }}\n { request body }\n ...
请求体为多个单行json数据用\n连接起来elasticsearch
action/metadata行,指定操做类型和参数post
action/metadata 行指定 哪个文档 作 什么操做 。 action/metadata 行指定 哪个文档 作 什么操做 。url
action 必须是如下选项之一:code
create 若是文档不存在,那么就建立它。 index 建立一个新文档或者替换一个现有的文档。 update 部分更新一个文档。 delete 删除一个文档。
metadata 应该 指定被索引、建立、更新或者删除的文档的 _index 、 _type 和 _idblog
例如,一个 delete 请求看起来是这样的:索引
{ "delete": { "_index": "website", "_type": "blog", "_id": "123" }}
一个create请求:文档
{ "create": { "_index": "website", "_type": "blog", "_id": "123" }} { "title": "My first blog post" }
create请求必须跟上一行,内容为建立的文档参数
若是不指定 _id ,将会自动生成一个 ID :
一个index请求:
{ "index": { "_index": "website", "_type": "blog" }} { "title": "My second blog post" }
把全部的操做组合在一块儿,一个完整的 bulk 请求 有如下形式:
POST /_bulk { "delete": { "_index": "website", "_type": "blog", "_id": "123" }} { "create": { "_index": "website", "_type": "blog", "_id": "123" }} { "title": "My first blog post" } { "index": { "_index": "website", "_type": "blog" }} { "title": "My second blog post" } { "update": { "_index": "website", "_type": "blog", "_id": "123", "_retry_on_conflict" : 3} } { "doc" : {"title" : "My updated blog post"} }
bulk请求返回的结构类型以下:
{ "took": 4, "errors": false, "items": [ { "delete": { "_index": "website", "_type": "blog", "_id": "123", "_version": 2, "status": 200, "found": true }}, { "create": { "_index": "website", "_type": "blog", "_id": "123", "_version": 3, "status": 201 }}, { "create": { "_index": "website", "_type": "blog", "_id": "EiwfApScQiiy7TIKFxRCTw", "_version": 1, "status": 201 }}, { "update": { "_index": "website", "_type": "blog", "_id": "123", "_version": 4, "status": 200 }} ] }
items为一个列表,依次是请求操做的返回结果
另外能够将metadata中的索引,类型,文档id均可以放在url中 metadata中若是有会覆盖url中指定的索引类型和id