一,108-全文检索-ElasticSearch-入门-put&post修改数据
第一种更新方式:
dart
POST customer/external/1/_update
{
"doc":{
"name": "John Doew"
}
}
第二种更新方式:
dart
POST customer/external/1
{
"name": "John Doe2"
}
第三种更新方式:
dart
PUT customer/external/1
{
"name": "John Doe"
}
区别:
-
第一种POST操作会对比源文档数据,如果相同不会有什么操作,文档 version 不增加
-
第二种和第三种操作总会将数据重新保存并增加 version 版本
此外,还可以实现更新同时增加属性。
dart
POST customer/external/1/_update
{
"doc": { "name": "Jane Doe", "age": 20 }
}
二,109-全文检索-ElasticSearch-入门-删除数据&bulk批量操作导入样本测试数据
1,删除一条数据
dart
DELETE customer/_doc/1
删除customer索引下id为1的文档。
2,删除整个索引
dart
DELETE customer
3,批量导入数据
Elasticsearch的_bulk
API允许你执行批量操作,这包括索引、更新或删除多个文档。批量操作可以显著提高性能,因为它减少了网络往返次数和资源消耗。
以下是使用_bulk
API的示例:
-
批量索引文档 :
下面的示例展示了如何使用
_bulk
API同时索引两个文档:jsonPOST /_bulk { "index" : { "_index" : "customer", "_type" : "external" } } { "name": "John Doe" } { "index" : { "_index" : "customer", "_type" : "external" } } { "name": "Jane Smith" }
在这个示例中,我们发送了一个
_bulk
请求,包含了两个索引操作。每个索引操作都由两行组成:第一行是一个JSON对象,指定了索引操作的参数(如索引名和类型),第二行是要索引的文档。 -
批量更新文档 :
使用
_update
操作可以实现批量更新:jsonPOST /_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,并提供了要更新的字段。 -
混合操作 :
_bulk
API允许在同一个请求中混合不同类型的操作:jsonPOST /_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导入如下数据。
dart
POST bank/acount/_bulk
测试数据比较多,完整数据参考https://blog.csdn.net/CB_Beginner/article/details/121199142,如下:
dart
{"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"}}