【ElasticSearch(四)】PUT&POST更新数据、DELETE删除数据、_bulk批量操作
【ElasticSearch(四)】PUT/POST更新数据、DELETE删除数据、_bulk批量操作
先查询下现在的情况GET http://localhost:9200/customer/external/1
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 3,
"_seq_no": 5,
"_primary_term": 1,
"found": true,
"_source": {
"name": 1
}
}
一、PUT / POST更新数据
总结:
-
POST更新操作
如果URL带
_update
,更新前会比对新旧数据,如果新旧数据完全相同,将不会进行任何操作noop,不会影响序列号、版本号信息。如果URI不带
_update
,不会检查原数据,都会显示updated -
PUT路径没法带
_update
,每次都会更新显示updated
1.POST访问地址:(路径带_update)
POST customer/external/1/_update
Body数据:
路径带_update
,要在参数外套一层doc
{
"doc":{
"name":2
}
}
返回结果:更新成功
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 4,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 7,
"_primary_term": 2
}
当我们再次发送请求时(Body数据相同),会发现200请求成功,但是版本号没有发生变化,result变成了noop(no operation)表示没有发生变化。
现在修改Body数据
{
"doc":{
"name":3
}
}
再次发送请求,返回结果。updated
2.POST访问地址:(路径不带_update)
POST customer/external/1
返回结果:尽管name参数和原来一样,但还是会updated的。
3.PUT访问地址:
(注意PUT访问路径是不能加_update
的)
PUT customer/external/1
这个测试结果和 POST customer/external/1 一样
二、PUT / POST增加属性更新
增加属性更新和上方规则相同,传参直接添加属性就可以保存。
下面仅以PUT为例:
PUT customer/external/1
Body请求数据:
{
"name": "xiaoming",
"age": 13
}
再次使用GET查询返回结果:
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 8,
"_seq_no": 11,
"_primary_term": 2,
"found": true,
"_source": {
"name": "xiaoming",
"age": 13
}
}
三、DELETE删除数据信息
DELETE http://localhost:9200/customer/external/1/
返回结果:result变为deleted
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 9,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 12,
"_primary_term": 2
}
再用GET方法查询,返回404没找到:found为false
四、DELETE删除整个索引
DELETE http://localhost:9200/customer
返回200,结果:
{
"acknowledged": true
}
再用GET方法查询,返回404错误:找不到索引
{
"error": {
"root_cause": [
{
"type": "index_not_found_exception",
"reason": "no such index [customer]",
"resource.type": "index_or_alias",
"resource.id": "customer",
"index_uuid": "_na_",
"index": "customer"
}
],
"type": "index_not_found_exception",
"reason": "no such index [customer]",
"resource.type": "index_or_alias",
"resource.id": "customer",
"index_uuid": "_na_",
"index": "customer"
},
"status": 404
}
注意:ES中并不能删除索引中的类型,只能删除整个索引和数据信息。
五、_bulk批量操作
批量操作中的每一个操作相互独立,可以独立成功或失败,彼此没有影响(不像mysql,批量操作中的一条执行失败,后面就不会执行了)。
1.请求的语法格式
这里有两条数据,数据之间可以没有空行。
POST 索引/类型/_bulk
{action:{metadata}}\n
{request body}\n
{action:{metadata}}\n
{request body}\n
action
:操作,可以是create
(创建),index
(保存),update
(更新),delete
(删除)等
metadata
:元数据,可以写数据的"_id"
等
request body
:写数据本身
2.测试工具
由于PostMan无法完成本功能,会出现400错误,出错原因是由于PostMan自动优化了换行符号。
我们需要采用Kibana测试。
访问http://localhost:5601,选择Kibana,选择侧边栏的Dev Tools
界面长这个样子。点击执行箭头,就可以发送请求
3.测试案例
【案例一】
POST /customer/external/_bulk
{"index":{"_id":"1"}}
{"name": "JSON Doe"}
{"index":{"_id":"2"}}
{"name": "Jone Doe"}
返回结果:
took
:完成请求花费的时间,毫秒
errors
:是否出现了错误
items
:返回的结果
#! [types removal] Specifying types in bulk requests is deprecated.
{
"took" : 40,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "customer",
"_type" : "external",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1,
"status" : 201
}
}
]
}
【案例二】对整个ES进行批量操作
POST /_bulk
{"delete": {"_index": "website", "_type": "blog", "_id": "123"}}
{"create": {"_index": "website", "_type": "blog", "_id": "123"}}
{"title": "My first blog post"}
{"index": {"_index": "website", "_type": "blog", "_id": "123"}}
{"title": "My second blog post"}
{"update": {"_index": "website", "_type": "blog", "_id": "123"}}
{"doc": {"title": "My updated blog post"}}
我这里不小心执行了两次这个批量操作
返回结果:
#! [types removal] Specifying types in bulk requests is deprecated.
{
"took" : 6,
"errors" : false,
"items" : [
{
"delete" : {
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 5,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 1,
"status" : 200
}
},
{
"create" : {
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 6,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 5,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 7,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 6,
"_primary_term" : 1,
"status" : 200
}
},
{
"update" : {
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 8,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 7,
"_primary_term" : 1,
"status" : 200
}
}