- bulk请求是独立的,每一条的失败与否 都不会影响到其他的请求。
- bulk 支持多种操作,如下create、index、update、delete。
- create 如果文档不存在就创建,但如果文档存在就返回错误
- index 如果文档不存在就创建,如果文档存在就更新
- update 更新一个文档,如果文档不存在就返回错误
- delete 删除一个文档,如果要删除的文档id不存在,就返回错误
- 语法结构上,一个操作分为两部分,一部分指定操作类型和索引,另一部分是请求体;
- 每个部分的json必须为一行。同时针对不同的操作类型,第二行的请求体是不一样的
- (1)index 和 create 第二行是source数据体
- (2)delete 没有第二行
- (3)update 第二行可以是partial doc,upsert或者是script
示例:
如果一个部分的json不在一行,会报错
POST /forum/article/_bulk
{ "create": { "_id": 20 }}
{ "articleID" : "XHDK-A-1293-#fJ3",
"userID" : 1, "hidden": false, "postDate": "2017-01-01" }
//报错
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Malformed action/metadata line [3], expected START_OBJECT but found [VALUE_STRING]"
}
],
"type": "illegal_argument_exception",
"reason": "Malformed action/metadata line [3], expected START_OBJECT but found [VALUE_STRING]"
},
"status": 400
}
create
POST /forum/article/_bulk
{ "create": { "_id": 5 }}
{ "articleID" : "XHDK-A-1293-#fJ3", "userID" : 1, "hidden": false, "postDate": "2017-01-01" }
//因为 5已经存在,所以create报错
{
"took": 311,
"errors": true,
"items": [
{
"create": {
"_index": "forum",
"_type": "article",
"_id": "5",
"status": 409,
"error": {
"type": "version_conflict_engine_exception",
"reason": "[article][5]: version conflict, document already exists (current version [8])",
"index_uuid": "7P_smfX_Q8adsRsMDRcpFA",
"shard": "1",
"index": "forum"
}
}
}
]
}
//同理文档id,成功
POST /forum/article/_bulk
{ "create": { "_id": 20 }}
{ "articleID" : "XHDK-A-1293-#fJ3", "userID" : 1, "hidden": false, "postDate": "2017-01-01" }
//成功
{
"took": 2506,
"errors": false,
"items": [
{
"create": {
"_index": "forum",
"_type": "article",
"_id": "20",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 20,
"_primary_term": 10,
"status": 201
}
}
]
}
index
POST /forum/article/_bulk
{ "index": { "_id": 5 }}
{ "articleID" : "XHDK-A-1293-#fJ3","userID" : 1, "hidden": false, "postDate": "2017-01-01" }
//index 比较实用了,有就更新,没有就创建
{
"took": 431,
"errors": false,
"items": [
{
"index": {
"_index": "forum",
"_type": "article",
"_id": "5",
"_version": 9,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 8,
"_primary_term": 10,
"status": 200
}
}
]
}
update
//doc
POST /forum/article/_bulk
{ "update": { "_id": 5 }}
{"doc":{ "articleID" : "XHDK-A-1293-#fJ3","userID" : 1, "hidden": false, "postDate": "2017-01-01" }}
//返回结果
{
"took": 1,
"errors": false,
"items": [
{
"update": {
"_index": "forum",
"_type": "article",
"_id": "5",
"_version": 9,
"result": "noop",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"status": 200
}
}
]
}
//upsert或者是script
delete
POST /forum/article/_bulk
{ "delete": { "_id": 20 }}
//delete是根据文档id删除数据
{
"took": 375,
"errors": false,
"items": [
{
"delete": {
"_index": "forum",
"_type": "article",
"_id": "20",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 21,
"_primary_term": 10,
"status": 200
}
}
]
}
引用: