当前位置: 首页 > news >正文

谷粒商城实战笔记-108~109-elasticsearch删除与批量导入

一,108-全文检索-ElasticSearch-入门-put&post修改数据

第一种更新方式:

POST customer/external/1/_update
{"doc":{"name": "John Doew"}
}

第二种更新方式:

POST customer/external/1
{
"name": "John Doe2"
}

第三种更新方式:

PUT customer/external/1
{
"name": "John Doe"
}

区别:

  • 第一种POST操作会对比源文档数据,如果相同不会有什么操作,文档 version 不增加

  • 第二种和第三种操作总会将数据重新保存并增加 version 版本

此外,还可以实现更新同时增加属性。

POST customer/external/1/_update
{"doc": { "name": "Jane Doe", "age": 20 }
}

二,109-全文检索-ElasticSearch-入门-删除数据&bulk批量操作导入样本测试数据

1,删除一条数据

DELETE customer/_doc/1

删除customer索引下id为1的文档。

2,删除整个索引

DELETE customer

3,批量导入数据

Elasticsearch的_bulk API允许你执行批量操作,这包括索引、更新或删除多个文档。批量操作可以显著提高性能,因为它减少了网络往返次数和资源消耗。

以下是使用_bulk API的示例:

  1. 批量索引文档
    下面的示例展示了如何使用_bulk API同时索引两个文档:

    POST /_bulk
    { "index" : { "_index" : "customer", "_type" : "external" } }
    { "name": "John Doe" }
    { "index" : { "_index" : "customer", "_type" : "external" } }
    { "name": "Jane Smith" }
    

    在这个示例中,我们发送了一个_bulk请求,包含了两个索引操作。每个索引操作都由两行组成:第一行是一个JSON对象,指定了索引操作的参数(如索引名和类型),第二行是要索引的文档。

  2. 批量更新文档
    使用_update操作可以实现批量更新:

    POST /_bulk
    { "update" : {"_id" : "1", "_index" : "customer", "_type" : "external"} }
    { "doc" : { "name" : "John Updated" } }
    { "update" : {"_id" : "2", "_index" : "customer", "_type" : "external"} }
    { "doc" : { "name" : "Jane Updated" } }
    

    这里,我们发送了一个包含两个更新操作的_bulk请求。每个更新操作指定了要更新的文档ID,并提供了要更新的字段。

  3. 混合操作
    _bulk API允许在同一个请求中混合不同类型的操作:

    POST /_bulk
    { "index" : { "_index" : "customer", "_type" : "external" } }
    { "name": "First Document" }
    { "delete" : {"_id" : "3", "_index" : "customer", "_type" : "external"} }
    { "update" : {"_id" : "4", "_index" : "customer", "_type" : "external"} }
    { "doc" : { "name" : "Fourth Document Updated" } }
    

    在这个示例中,我们首先索引了一个文档,然后删除了一个文档,接着更新了另一个文档。

使用_bulk API时,需要注意以下几点:

  • 请求体中的操作必须交替进行,首先是操作定义,紧接着是操作数据。
  • 每个操作定义和操作数据之间用换行符\n分隔。
  • 批量请求的大小通常受到限制,因此需要根据Elasticsearch集群的性能和资源合理分批操作。

4,批量导入测试数据

使用bulk批量操作API导入如下数据。

POST bank/acount/_bulk

测试数据比较多,完整数据参考https://blog.csdn.net/CB_Beginner/article/details/121199142,如下:

{"index":{"_id":"1"}}
{"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
{"index":{"_id":"6"}}
{"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"hattiebond@netagy.com","city":"Dante","state":"TN"}
{"index":{"_id":"13"}}
{"account_number":13,"balance":32838,"firstname":"Nanette","lastname":"Bates","age":28,"gender":"F","address":"789 Madison Street","employer":"Quility","email":"nanettebates@quility.com","city":"Nogal","state":"VA"}
{"index":{"_id":"18"}}
{"account_number":18,"balance":4180,"firstname":"Dale","lastname":"Adams","age":33,"gender":"M","address":"467 Hutchinson Court","employer":"Boink","email":"daleadams@boink.com","city":"Orick","state":"MD"}
{"index":{"_id":"20"}}
{"account_number":20,"balance":16418,"firstname":"Elinor","lastname":"Ratliff","age":36,"gender":"M","address":"282 Kings Place","employer":"Scentric","email":"elinorratliff@scentric.com","city":"Ribera","state":"WA"}
{"index":{"_id":"25"}}
{"account_number":25,"balance":40540,"firstname":"Virginia","lastname":"Ayala","age":39,"gender":"F","address":"171 Putnam Avenue","employer":"Filodyne","email":"virginiaayala@filodyne.com","city":"Nicholson","state":"PA"}
{"index":{"_id":"32"}}
http://www.lryc.cn/news/418045.html

相关文章:

  • RabbitMQ:发送者的可靠性之使用消息确认回调
  • HCIP学习 | OSPF---LSA限制、不规则区域、附录E、选路
  • CVE-2017-15715~Apache解析漏洞【春秋云境靶场渗透】
  • thinkphp 5.0.24生成模块
  • 值得注意!家里有带毛发动物就有浮毛?宠物空气净化器一键净化
  • Linux 代理(proxy)设置
  • 操作系统真相还原:获取文件属性
  • 聚鼎装饰画:投资一家装饰画店铺要花费多少钱
  • 编程的魅力、其重要性、学习方法以及未来趋势
  • ubuntu init set
  • MySQL数据分析进阶(八)存储过程
  • 最深的根,
  • 【常见的设计模式】工厂模式
  • postgres收缩工具两种工具的使用对比
  • 仿真入门——CST软件如何设置分布式计算的共享储存
  • 【JVM基础17】——实践-说一下JVM调优工具
  • 【QT】Qt中Websocket的使用
  • 【vue3】【elementPlus】【国际化】
  • 用python实现求两个整数的最大公约数
  • Linux 内核源码分析---proc 文件系统
  • 视频号直播回放怎么下载?
  • 【第九节】python中xml解析和json编解码
  • yolo v8部署到云服务器问题记录
  • 端口被占用,杀死进程的步骤
  • 接口入门(企业常见使用,一分钟搞定版)
  • 深入解析:Cookie 与 Session 的区别及应用场景
  • LLM金融文本分类文档说明
  • EI检索,2天录用,3天见刊!截稿在即,这本水刊你还不投吗?
  • sql获取过去的小时数
  • 【Android Studio】彻底卸载