ES提供了Bulk API,可以执行批量操作,Bulk API允许使用在单个步骤中进行多次 create 、 index 、 update 或 delete 请求.牛逼的操作注定了他的不平凡,请求体的格式和正常的不一样~它长这个熊样.
{ action: { metadata }}\n
{ request body }\n
{ action: { metadata }}\n
{ request body }\n
...
说明
- 这种格式类似一个有效的单行
JSON
文档流,它通过换行符(\n)
连接到一起.包括最后一行,这些换行符被用作一个标记,可以有效分隔行. action/metadata
行指定某个文档做什么操作.metadata
指定被操作的文档的属性.如果_Type
,_id
等.request body
内指定文档字段的内容.是CREATE
的必要操作.- 本来打算和上一篇写在一起的.但是关于请求体这个破玩意可把我折腾死了.
TMD
请求方式的坑
先给展示一下我走的几个华丽丽的小坑吧.
HEAD插件
我也是服了这个插件了,它老验证我的多行json流不是个json干嘛!验证个锤子.看到都烦.md好气哦~锤子CEO老罗教育过我们:完成比完美更重要
.LZ日后再来收拾你!
CRUL请求1
# 为了方便阅读,请求换行了.
curl -XPOST http://127.0.0.1:8101/_bulk\?pretty
{ "create": { "_index": "small_video", "_type": "_doc", "_id": "1" }}
{ "name": "二滑大魔王1" ,"content":"hello1" }
{ "create": { "_index": "small_video", "_type": "_doc", "_id": "2" }}
{ "name": "二滑大魔王2" ,"content":"hello2" }
{ "create": { "_index": "small_video", "_type": "_doc", "_id": "3" }}
{ "name": "二滑大魔王3" ,"content":"hello3" }
然后我的终端给我报了个很好玩的错误~是不是深井冰
zsh: parse error near `}'
CRUL请求2
我整不了你个破终端我就我写个文件请求,tmd
vim test
# 添加
{ "create": { "_index": "small_video", "_type": "_doc", "_id": "1" }}
{ "name": "二滑大魔王1" ,"content":"hello1" }
{ "create": { "_index": "small_video", "_type": "_doc", "_id": "2" }}
{ "name": "二滑大魔王2" ,"content":"hello2" }
{ "create": { "_index": "small_video", "_type": "_doc", "_id": "3" }}
{ "name": "二滑大魔王3" ,"content":"hello3" }
# 以上是文件内容.别傻乎乎的全复制了哈
curl -XPOST http://127.0.0.1:8101/_bulk --data-binary @test
#请求报错
{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}%
你已经是个成熟的CURL了.你应该学会自己辨别它的类型!!!
解决方法就是正确请求方式的内容了.别慌往下看.
正确请求
CURL
# 为了方便阅读,我还是给他换行了~
curl -H "Content-Type: application/json"
-XPOST http://127.0.0.1:8101/_bulk --data-binary @test
批量创建
vim create
# 添加
{ "create": { "_index": "small_video", "_type": "_doc", "_id": "4" }}
{ "name": "二滑大魔王4" ,"content":"hello4" }
{ "create": { "_index": "small_video", "_type": "_doc", "_id": "5" }}
{ "name": "二滑大魔王5" ,"content":"hello5" }
{ "create": { "_index": "small_video", "_type": "_doc", "_id": "6" }}
{ "name": "二滑大魔王6" ,"content":"hello6" }
# 以上是文件内容.别傻乎乎的全复制了哈
请求地址
# 为了方便阅读,我还是给他换行了~
curl -H "Content-Type: application/json" -XPOST
http://127.0.0.1:8101/_bulk --data-binary @create
批量增删改
vim upd_del_cre
# 添加
{ "delete": { "_index": "small_video", "_type": "_doc", "_id": "6" }}
{ "update": { "_index": "small_video", "_type": "_doc", "_id": "5"} }
{ "doc" : {"name" : "123"} }
{ "create": { "_index": "small_video", "_type": "_doc", "_id":"7" }}
{ "name": "二滑大魔王7" ,"content":"hello7" }
# 以上是文件内容.别傻乎乎的全复制了哈
请求地址
# 为了方便阅读,我还是给他换行了~
curl -H "Content-Type: application/json" -XPOST
http://127.0.0.1:8101/_bulk --data-binary @upd_del_cre
当然也可以再请求地址中指定好索引和文档,例如
vim upd_del_cre
{ "delete": { "_index": "small_video", "_type": "_doc", "_id": "6" }}
{ "update": { "_index": "small_video", "_type": "_doc", "_id": "5"} }
{ "doc" : {"name" : "123"} }
{ "create": { "_index": "small_video", "_type": "_doc", "_id":"7" }}
{ "name": "二滑大魔王7" ,"content":"hello7" }
请求地址
# 为了方便阅读,我还是给他换行了~
curl -H "Content-Type: application/json" -XPOST
http://127.0.0.1:8101/small_video/_doc/_bulk --data-binary @new_create
我看你是个憨憨。