elasticsearch bulk批量增删改

朴灿烈づ我的快乐病毒、 2022-05-23 10:28 349阅读 0赞

一、bulk的操作类型

delete:删除一个文档,只要1个json串就可以了,其他都需要两个json串

  1. { "delete": { "_index":"test_index", "_type": "test_type", "_id": "2" }}

create:PUT /index/type/id/_create,强制创建,id已存在会失败,但不影响已成功的语句

  1. POST /_bulk
  2. { "create": { "_index":"test_index", "_type": "test_type", "_id": "12" }}
  3. { "score_num": 68, "tags":"my love" }

index:普通的put操作,可以是创建文档,也可以是全量替换文档

  1. POST /_bulk
  2. { "index": { "_index":"test_index", "_type": "test_type", "_id": "12" }}
  3. { "score_num": 68, "tags":"my love" }

update:执行的partial update操作

  1. POST /_bulk
  2. { "update": { "_index":"test_index", "_type": "test_type", "_id": "12" }}
  3. { "doc" : {
  4. "test_field2" : "bulk test1"} }

上面的 doc 是关键字

二、注意

注意:bulk api对json的语法,有严格的要求,每个json串不能换行,只能放一行,同时一个json串和一个json串之间,必须有一个换行。 bulk操作中,任意一个操作失败,是不会影响其他的操作的,但是在返回结果里,会告诉你异常日志

三、指定index

  1. POST /test_index/_bulk
  2. { "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

  1. POST /test_index/test_type/_bulk
  2. { "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之间。

发表评论

表情:
评论列表 (有 0 条评论,349人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Elasticsearch增删

    Elasticsearch的增删改查: 面向文档 document数据格式 1. 应用系统的数据结构都是面向对象的,复杂的 2. 对象数据存储到数据库中,只能拆

    相关 Elasticsearch增删

    面向文档 document数据格式 1. 应用系统的数据结构都是面向对象的,复杂的 2. 对象数据存储到数据库中,只能拆解开来,变为扁平的多张表,每次查询的时候还得