elasticsearch bulk批量增删改
一、bulk的操作类型
delete:删除一个文档,只要1个json串就可以了,其他都需要两个json串
{ "delete": { "_index":"test_index", "_type": "test_type", "_id": "2" }}
create:PUT /index/type/id/_create
,强制创建,id已存在会失败,但不影响已成功的语句
POST /_bulk
{ "create": { "_index":"test_index", "_type": "test_type", "_id": "12" }}
{ "score_num": 68, "tags":"my love" }
index:普通的put操作,可以是创建文档,也可以是全量替换文档
POST /_bulk
{ "index": { "_index":"test_index", "_type": "test_type", "_id": "12" }}
{ "score_num": 68, "tags":"my love" }
update:执行的partial update操作
POST /_bulk
{ "update": { "_index":"test_index", "_type": "test_type", "_id": "12" }}
{ "doc" : {
"test_field2" : "bulk test1"} }
上面的 doc 是关键字
二、注意
注意:bulk api对json的语法,有严格的要求,每个json串不能换行,只能放一行,同时一个json串和一个json串之间,必须有一个换行。 bulk操作中,任意一个操作失败,是不会影响其他的操作的,但是在返回结果里,会告诉你异常日志
三、指定index
POST /test_index/_bulk
{ "delete": { "_type": "test_type", "_id": "3" }} { "create": { "_type": "test_type", "_id": "5" }} { "test_field": "test5" } { "index": { "_type": "test_type" }} { "test_field": "auto-generate id test" } { "index": { "_type": "test_type", "_id": "2" }} { "test_field": "replaced test2" } { "update": { "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} } { "doc" : {"test_field2" : "bulk test1"} }
四、指定index、type
POST /test_index/test_type/_bulk
{ "delete": { "_id": "3" }} { "create": { "_id": "12" }} { "test_field":"test12" } { "index": { }} { "test_field":"auto-generate id test" } { "index":{ "_id": "2" }} { "test_field":"replaced test2" } { "update": { "_id": "1", "_retry_on_conflict" : 3} } { "doc" : {"test_field2" : "bulk test1"} }
五、ulk size最佳大小
bulk request会加载到内存里,如果太大的话,性能反而会下降,因此需要反复尝试一个最佳的bulk size。一般从1000~5000条数据开始,尝试逐渐增加。另外,如果看大小的话,最好是在5~15MB之间。
还没有评论,来说两句吧...