谷粒商城实战笔记-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"}}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小手追梦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值