es 的 bulk 操作,是用来批量发送请求,或者理解为批量操作的。
bulk 支持多种操作,如下create、index、update、delete。
语法结构上,一个操作分为两部分,一部分指定操作类型和索引,另一部分是请求体;
。
针对不同的操作类型,第二行的请求体是不一样的
(1)index 和 create 第二行是source数据体
(2)delete 没有第二行
(3)update 第二行可以是partial doc,upsert或者是script
- POST /_bulk
- {"create":{"_index":"索引名", "_id":"文档"}} // 动作
- {"field1":"value1"} //请求体
- POST /_bulk
- {"create":{"_index":"zm_blog5"}}
- {"name":"小刘","desc":{"simple":"山西人","complete":"山西人,定居太原","other":"程序员"}}
- {"create":{"_index":"zm_blog5"}}
- {"name":"小刘","desc":{"simple":"山西人","complete":"山西人,定居太原","other":"程序员"}}
- POST zm_blog5/_bulk
- {"create":{}}
- {"name":"小刘","desc":{"simple":"山西人","complete":"山西人,定居太原","other":"程序员"}}
- {"create":{}}
- {"name":"小刘","desc":{"simple":"山西人","complete":"山西人,定居太原","other":"程序员"}}
执行结果是分开的,比如发送2个操作,两个操作结果分别返回,可能其中一个失败另一个成功
比如执行:
- POST /_bulk
- {"create":{"_index":"zm_blog5"}}
- {"name":"小刘","desc":{"simple":"山西人","complete":"山西人,定居太原","other":"程序员"}}
- {"create":{"_index":"zm_blog5"}}
- {"name":"小刘","desc":{"simple":"山西人","complete":"山西人,定居太原","other":"程序员"}}
返回结果:
每个返回结果中有 http status,以及其他详细信息。
- {
- "took" : 8,
- "errors" : false,
- "items" : [
- {
- "create" : {
- "_index" : "zm_blog5",
- "_type" : "_doc",
- "_id" : "wKNBpYQBCuglFCwN_ObP",
- "_version" : 1,
- "result" : "created",
- "_shards" : {
- "total" : 1,
- "successful" : 1,
- "failed" : 0
- },
- "_seq_no" : 1,
- "_primary_term" : 1,
- "status" : 201
- }
- },
- {
- "create" : {
- "_index" : "zm_blog5",
- "_type" : "_doc",
- "_id" : "waNBpYQBCuglFCwN_ObP",
- "_version" : 1,
- "result" : "created",
- "_shards" : {
- "total" : 1,
- "successful" : 1,
- "failed" : 0
- },
- "_seq_no" : 2,
- "_primary_term" : 1,
- "status" : 201
- }
- }
- ]
- }
作者:zhimin_
链接:https://www.jianshu.com/p/60a792037f8c
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。