bulk的格式
{action:{metadata}}\n
{requstbody}\n
action(行为)
- create:文档不存在时创建
- update:更新文档
- index:创建新文档或替换已有文档
- delete:删除一个文档
metadata : _index,_type,id
create和index的区别
如果数据存在,使用create操作失败,提示文档已经存在,使用index则可以成功执行。
示例:
{"delete" : {"_index":"lib","_type" : "user","id":"1"}}
批量添加:
POST /lib2/_bulk
{"index":{"_id":1}}
{"title":"Java","price":1}
{"index":{"_id":2}}
{"title":"Html5","price":2}
{"index":{"_id":3}}
{"title":"Php","price":3}
{"index":{"_id":4}}
{"title":"Python","price":4}
#结果:errors字段是false代表成功了
{
"took" : 65,
"errors" : false,
}
#获取
GET /lib2/_mget
{
"ids":["1","2","3","4"]
}
不同类型的操作同时执行
#6.x.x及之前的版本,带_type
POST /lib2/_bulk
{"delete":{"_index":"lib2","_type":"books","_id":"4"}} #删除没有请求体
{"create":{"_index":"tt","_type":"ttt","_id":"100"}}
{"name":"lisi"}
{"index":{"_index":"lib2","_type":"books","_id":"4"}}
{"name":"zhaosi"}
{"update":{"_index":"lib2","_type":"books","_id":"4"}}
{"doc":{"price":58}}
#7.0.0版本开始没有_type
POST /lib2/_bulk
{"delete":{"_index":"lib2","_id":"4"}}
{"create":{"_index":"tt","_id":"100"}}
{"name":"lisi"}
{"index":{"_index":"lib2","_id":"4"}}
{"name":"zhaosi"}
{"update":{"_index":"lib2","_id":"4"}}
{"doc":{"price":58}}
bulk—次最大处理多少数据量
bulk会把要处理的致数据放入内存中,所以数据量是有限的,最佳的数据量不是一个确定的数值,它取决于你的硬件,文档的大小及复杂性,你的索引以及搜索的负载。一般议是1000-5000个文档,大小建议是5-15MB, 默认不能超过100M, 可以在es配置文件($ES_HOME/config/elasticsearch.yml)中配置。
网友评论