1.语法
POST /bulk{"delete":{"_index":"test_index","_type":"test_type","_id":9}}
{"create":{"_index":"test_index","_type":"test_type","_id":11}}
{"test_field":"test201712291140"}
{"index":{"_index":"test_index","_type":"test_type","_id":11}}
{"test_field":"test201712291141 replaced"}
{"update":{"_index":"test_index","_type":"test_type","_id":8,"_retry_on_conflict":3}}
{"doc":{"test_field2":"xxx00000000"}}
每一个操作有两个json串:(除了delete操作)
{“action”:{"metadata"}
{"data"}
举例,创建文档
{"index":{"_index":"test_index","_type":"test_type","_id":11}
{"test_field":"test","test_field2":"test2"}
操作类型
delete 删除一个文档操作,只要一个json串即可
update 执行partial update操作
create put /index/type/id/_create,强制创建操作
index 普通的put操作,可以是创建文档,也可以是全量替换
bulk api对json 的语法有严格的要求,每个json串不能换行,同时一个json串和一个json串之间,必须有一个换行
POST /_bulk
{
"delete":{
"_index":"test_index",
"_type":"test_type",
"_id":11
}
}
上面的bulk api语法有误,报错:
{
"error": {
"root_cause": [
{
"type": "json_e_o_f_exception",
"reason": "Unexpected end-of-input: expected close marker for Object (start marker at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@bad5eb; line: 1, column: 1])\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@bad5eb;
line: 1, column: 3]"
}
],
"type": "json_e_o_f_exception",
"reason": "Unexpected end-of-input: expected close marker for Object (start marker at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@bad5eb; line: 1, column: 1])\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@bad5eb;
line: 1, column: 3]"
},
"status": 500
}
2.例子
POST /_bulk
{"delete":{"_index":"test_index","_type":"test_type","_id":9}}
{"create":{"_index":"test_index","_type":"test_type","_id":11}}
{"test_field":"test201712291140"}
{"index":{"_index":"test_index","_type":"test_type","_id":11}}
{"test_field":"test201712291141 replaced"}
{"update":{"_index":"test_index","_type":"test_type","_id":8,"_retry_on_conflict":3}}
{"doc":{"test_field2":"xxx00000000"}}
执行结果
#! Deprecation: Deprecated field [_retry_on_conflict] used, expected [retry_on_conflict] instead
{
"took": 32,
"errors": false,
"items": [
{
"delete": {
"_index": "test_index",
"_type": "test_type",
"_id": "9",
"_version": 1,
"result": "not_found",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 13,
"_primary_term": 4,
"status": 404
}
},
{
"create": {
"_index": "test_index",
"_type": "test_type",
"_id": "11",
"_version": 6,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 7,
"_primary_term": 4,
"status": 201
}
},
{
"index": {
"_index": "test_index",
"_type": "test_type",
"_id": "11",
"_version": 7,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 8,
"_primary_term": 4,
"status": 200
}
},
{
"update": {
"_index": "test_index",
"_type": "test_type",
"_id": "8",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 14,
"_primary_term": 4,
"status": 200
}
}
]
}
查询每个操作的结果:
GET /test_index/test_type/10
{
"_index": "test_index",
"_type": "test_type",
"_id": "9",
"found": false
}
GET /test_index/test_type/11
{
"_index": "test_index",
"_type": "test_type",
"_id": "11",
"_version": 7,
"found": true,
"_source": {
"test_field": "test201712291141 replaced"
}
}
GET /test_index/test_type/8
{
"_index": "test_index",
"_type": "test_type",
"_id": "8",
"_version": 3,
"found": true,
"_source": {
"test_field": "xxx81",
"test_field2": "xxx00000000"
}
}