【ElasticSearch】bulk批量操作文档:添加、更新、删除
【ElasticSearch】bulk批量操作文档:添加、更新、删除
======================================================
1、索引 index
2、创建 create
3、index 和 create 区别
4、更新 update
5、删除 delete
======================================================
1、索引 index
创建新文档或替换已用文档
vim customer_index.json { "index" : { "_index" : "customer", "_id" : "1" } } { "name" : "Tom" } { "index" : { "_index" : "customer", "_id" : "2" } } { "name" : "Bob" } curl -H "Content-Type: application/json" -XPOST "localhost:9200/customer/_bulk?pretty&refresh" --data-binary "@customer_index.json" curl "localhost:9200/_cat/indices?v"
POST myindex/_bulk
{ "index":{"_id":1} }
{ "articleID":"1001", "title":"西安", "publishDate":"2021-05-01", "createdBy":"21" }
{ "index":{"_id":2} }
{ "articleID":"1002", "title":"成都", "publishDate":"2021-05-01", "createdBy":"30"}
{ "index":{"_id":3} }
{ "articleID":"1003", "title":"重庆", "publishDate":"2021-05-02", "createdBy":"21"}
2、创建 create
文档不存在时创建
vim customer_create.json { "create" : { "_index" : "customer", "_id" : "3" } } { "name" : "Alice" } { "create" : { "_index" : "customer", "_id" : "4" } } { "name" : "Smith" } curl -H "Content-Type: application/json" -XPOST "localhost:9200/customer/_bulk?pretty&refresh" --data-binary "@customer_create.json" curl "localhost:9200/_cat/indices?v"
3、index 和 create 区别
如果数据存在,使用create操作失败,会提示文档已存在,使用index则可以成功执行
{ "took" : 44, "errors" : true, "items" : [ { "create" : { "_index" : "customer", "_type" : "_doc", "_id" : "1", "status" : 409, "error" : { "type" : "version_conflict_engine_exception", "reason" : "[1]: version conflict, document already exists (current version [1])", "index_uuid" : "SaYbL9kjSIKcXY1B73U9Rw", "shard" : "0", "index" : "customer" } } } ] }
4、更新 update
vim customer_update.json { "update" : {"_id" : "1", "_index" : "customer"} } { "doc" : {"name" : "Tom Green"} } { "update" : {"_id" : "2", "_index" : "customer"} } { "doc" : {"name" : "Bob Smith"} } curl -H "Content-Type: application/json" -XPOST "localhost:9200/customer/_bulk?pretty&refresh" --data-binary "@customer_update.json" curl "localhost:9200/_cat/indices?v"
doc 是关键字保持
5、删除 delete
vim customer_delete.json { "delete" : { "_index" : "customer", "_id" : "1" } } { "delete" : { "_index" : "customer", "_id" : "2" } } curl -H "Content-Type: application/json" -XPOST "localhost:9200/customer/_bulk?pretty&refresh" --data-binary "@customer_delete.json" curl "localhost:9200/_cat/indices?v"
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端