跟我学Elasticsearch(13) 使用bulk进行批量增删改

使用bulk语法可以进行批量增删改操作,bulk语法有严格的要求,每个json串不能换行,两个json串必须换行,下面演示下

#删除
POST /_bulk
{ "delete": { "_index": "test_index", "_type": "test_type", "_id": "1" }} 
#强制创建
POST /_bulk
{ "create": { "_index": "test_index", "_type": "test_type", "_id": "1" }}
{ "test_field":    "create test" }
#不存在则创建,存在则替换,执行put操作
POST /_bulk
{ "index":  { "_index": "test_index", "_type": "test_type", "_id": "1" }}
{ "test_field":    "index test" }
#更新,执行partial update操作
POST /_bulk
{ "update": { "_index": "test_index", "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} }
{ "doc" : {"test_field" : "update test"} }

我们也可以把增删改放在1个请求里面

POST /_bulk
{ "delete": { "_index": "test_index", "_type": "test_type", "_id": "1" }} 
{ "create": { "_index": "test_index", "_type": "test_type", "_id": "1" }}
{ "test_field":    "create test" }
{ "index":  { "_index": "test_index", "_type": "test_type", "_id": "1" }}
{ "test_field":    "index test" }
{ "update": { "_index": "test_index", "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} }
{ "doc" : {"test_field" : "update test"} }

需要注意的是,bulk请求会把document加载到内存,如果一次批量操作的数据过大反而会影响性能。我们可以从1000-5000条数据开始尝试增加。如果看大小的话最好控制在5-15M。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值