ElasticSearch7.3学习(三)----采用restful风格 批量(bulk)增删改

本文介绍了如何利用Elasticsearch的_Bulk API进行文档增删改查的高效操作,避免大体积Json内存消耗。涵盖了创建、删除、更新等操作的语法和注意事项,以及实际使用中的性能建议。

Bulk 操作是将文档的增删改查一些列操作,通过一次请求全都做完。目的是减少网络传输次数。

语法:

POST /_bulk
{ "action" : { "metadata" }}
{ "data" }

如下操作,创建14,创建5,删除5,更新14

POST /_bulk
{  "create" : {  "_index" :  "test_index" ,   "_id" :  "14" }}
{  "test_field" :  "test14" }
{  "create" : {  "_index" :  "test_index" ,   "_id" :  "5" }}
{  "test_field" :  "test14" }
{  "delete" : {  "_index" :  "test_index" ,   "_id" :  "5" }} 
{  "update" : {  "_index" :  "test_index" ,   "_id" :  "14" } }
{  "doc" : { "test_field" :  "bulk test" } }

结果

{
  "took" :  1520 ,
  "errors" :  false ,
  "items" : [
    {
      "create" : {
        "_index" :  "test_index" ,
        "_type" :  "_doc" ,
        "_id" :  "14" ,
        "_version" :  1 ,
        "result" :  "created" ,
        "_shards" : {
          "total" :  2 ,
          "successful" :  1 ,
          "failed" :  0
        },
        "_seq_no" :  0 ,
        "_primary_term" :  1 ,
        "status" :  201
      }
    },
    {
      "create" : {
        "_index" :  "test_index" ,
        "_type" :  "_doc" ,
        "_id" :  "5" ,
        "_version" :  1 ,
        "result" :  "created" ,
        "_shards" : {
          "total" :  2 ,
          "successful" :  1 ,
          "failed" :  0
        },
        "_seq_no" :  1 ,
        "_primary_term" :  1 ,
        "status" :  201
      }
    },
    {
      "delete" : {
        "_index" :  "test_index" ,
        "_type" :  "_doc" ,
        "_id" :  "5" ,
        "_version" :  2 ,
        "result" :  "deleted" ,
        "_shards" : {
          "total" :  2 ,
          "successful" :  1 ,
          "failed" :  0
        },
        "_seq_no" :  2 ,
        "_primary_term" :  1 ,
        "status" :  200
      }
    },
    {
      "update" : {
        "_index" :  "test_index" ,
        "_type" :  "_doc" ,
        "_id" :  "14" ,
        "_version" :  2 ,
        "result" :  "updated" ,
        "_shards" : {
          "total" :  2 ,
          "successful" :  1 ,
          "failed" :  0
        },
        "_seq_no" :  3 ,
        "_primary_term" :  1 ,
        "status" :  200
      }
    }
  ]
}

总结:

为啥不采用Java里面传统的Json对象去实现批量操作,原因为解析Json字符串的时候,会保留一个比较大的Json对象放在Java内存中,大数据量的时候明显不可取。因此按照普通字符串读取就OK了。

1. 功能:

  • delete:删除一个文档,只要1个json串就可以了
  • create:相当于强制创建 PUT /index/type/id/_create
  • index:普通的put操作,可以是创建文档,也可以是全量替换文档
  • update:执行的是局部更新partial update操作

2. 格式:每个json不能换行。相邻json必须换行。

3. 隔离:每个操作互不影响。操作失败的行会返回其失败信息。

4. 实际用法:bulk请求一次不要太大,否则一下积压到内存中,性能会下降。所以,一次请求几千个操作、大小在几M正好。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

|旧市拾荒|

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值