【ElasticSearch】Part5 - 批量增删改(bulk)

在这里插入图片描述

批量删除

POST /_bulk
{"delete":{"_index":"article","_type":"poems","_id":"1"}}
{"delete":{"_index":"article","_type":"poems","_id":"2"}}
{"delete":{"_index":"article","_type":"poems","_id":"3"}}
{"delete":{"_index":"article","_type":"poems","_id":"4"}}
{"delete":{"_index":"article","_type":"poems","_id":"5"}}

批量新增(注意格式,数据json串在同一行)

POST /_bulk
{"create":{"_index":"article","_type":"poems","_id":"1"}}
{"title":"静夜思","author":"李白","dynasty":"唐","words":20,"tags":["月亮","思乡","五言绝句"],"content":"床前明月光,疑是地上霜。举头望明月,低头思故乡。"}
{"create":{"_index":"article","_type":"poems","_id":"2"}}
{"title":"悯农","author":"李绅","dynasty":"唐","words":20,"tags":["农耕","五言绝句"],"content":"春种一粒粟,秋收万颗子。四海无闲田,农夫犹饿死。"}
{"create":{"_index":"article","_type":"poems","_id":"3"}}
{"title":"春夜喜雨","author":"杜甫","dynasty":"唐","words":40,"tags":["春雨","五言律诗"],"content":"好雨知时节,当春乃发生。随风潜入夜,润物细无声。野径云俱黑,江船火独明。晓看红湿处,花重锦官城。"}
{"create":{"_index":"article","_type":"poems","_id":"4"}}
{"title":"望庐山瀑布","author":"李白","dynasty":"唐","words":20,"tags":["瀑布","七言绝句"],"content":"日照香炉生紫烟,遥看瀑布挂前川。飞流直下三千尺,疑是银河落九天。"}
{"create":{"_index":"article","_type":"poems","_id":"5"}}
{"title":"早发白帝城","author":"李白","dynasty":"唐","words":28,"tags":["船","七言绝句","白帝城"],"content":"朝辞白帝彩云间,千里江陵一日还。两岸猿声啼不住,轻舟已过万重山。"}

批量删除又新增

POST /_bulk
{"delete":{"_index":"article","_type":"poems","_id":"1"}}
{"delete":{"_index":"article","_type":"poems","_id":"2"}}
{"delete":{"_index":"article","_type":"poems","_id":"3"}}
{"delete":{"_index":"article","_type":"poems","_id":"4"}}
{"delete":{"_index":"article","_type":"poems","_id":"5"}}
{"create":{"_index":"article","_type":"poems","_id":"1"}}
{"title":"静夜思","author":"李白","dynasty":"唐","words":20,"tags":["月亮","思乡","五言绝句"],"content":"床前明月光,疑是地上霜。举头望明月,低头思故乡。"}
{"create":{"_index":"article","_type":"poems","_id":"2"}}
{"title":"悯农","author":"李绅","dynasty":"唐","words":20,"tags":["农耕","五言绝句"],"content":"春种一粒粟,秋收万颗子。四海无闲田,农夫犹饿死。"}
{"create":{"_index":"article","_type":"poems","_id":"3"}}
{"title":"春夜喜雨","author":"杜甫","dynasty":"唐","words":40,"tags":["春雨","五言律诗"],"content":"好雨知时节,当春乃发生。随风潜入夜,润物细无声。野径云俱黑,江船火独明。晓看红湿处,花重锦官城。"}
{"create":{"_index":"article","_type":"poems","_id":"4"}}
{"title":"望庐山瀑布","author":"李白","dynasty":"唐","words":20,"tags":["瀑布","七言绝句"],"content":"日照香炉生紫烟,遥看瀑布挂前川。飞流直下三千尺,疑是银河落九天。"}
{"create":{"_index":"article","_type":"poems","_id":"5"}}
{"title":"早发白帝城","author":"李白","dynasty":"唐","words":28,"tags":["船","七言绝句","白帝城"],"content":"朝辞白帝彩云间,千里江陵一日还。两岸猿声啼不住,轻舟已过万重山。"}

批量修改

POST /_bulk
{"update":{"_index":"article","_type":"poems","_id":"1"}}
{"doc":{"title":"静夜思","author":"李白","dynasty":"唐","words":20,"tags":["月亮","思乡","五言绝句"],"content":"被我改了"}}
{"update":{"_index":"article","_type":"poems","_id":"2"}}
{"doc":{"title":"悯农","author":"李绅","dynasty":"唐","words":20,"tags":["农耕","五言绝句"],"content":"被我改了"}}
{"update":{"_index":"article","_type":"poems","_id":"3"}}
{"doc":{"title":"春夜喜雨","author":"杜甫","dynasty":"唐","words":40,"tags":["春雨","五言律诗"],"content":"被我改了"}}
{"update":{"_index":"article","_type":"poems","_id":"4"}}
{"doc":{"title":"望庐山瀑布","author":"李白","dynasty":"唐","words":20,"tags":["瀑布","七言绝句"],"content":"被我改了"}}
{"update":{"_index":"article","_type":"poems","_id":"5"}}
{"doc":{"title":"早发白帝城","author":"李白","dynasty":"唐","words":28,"tags":["船","七言绝句","白帝城"],"content":"被我改了"}}

放到一起

POST /_bulk
{"delete":{"_index":"article","_type":"poems","_id":"1"}}
{"create":{"_index":"article","_type":"poems","_id":"1"}}
{"title":"静夜思","author":"李白","dynasty":"唐","words":20,"tags":["月亮","思乡","五言绝句"],"content":"床前明月光,疑是地上霜。举头望明月,低头思故乡。"}
{"update":{"_index":"article","_type":"poems","_id":"1"}}
{"doc":{"title":"静夜思","author":"李白","dynasty":"唐","words":20,"tags":["月亮","思乡","五言绝句"],"content":"被我叕改了"}}

不同的写法

POST article/poems/_bulk    //这里指定了index和type
{"delete":{"_id":"1"}}
{"delete":{"_id":"2"}}
{"delete":{"_id":"3"}}
{"delete":{"_id":"4"}}
{"delete":{"_id":"5"}}

或者

POST article/_bulk    //这里指定了index没指定type
{"delete":{"_type":"poems","_id":"1"}}
{"delete":{"_type":"poems","_id":"2"}}
{"delete":{"_type":"poems","_id":"3"}}
{"delete":{"_type":"poems","_id":"4"}}
{"delete":{"_type":"poems","_id":"5"}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值