一、PUT / POST更新数据
POST访问地址(PUT访问提示405方法不允许):
http://127.0.0.1:9200/customer/external/1/_update
Body数据:
{
"doc":{
"name": "JSON D0oe"
}
}
返回结果:
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 6,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 6,
"_primary_term": 1
}
当我们再次发送请求时会发现请求成功,但是版本号没有发生变化,返回结果中result变成了noop,表示没有发生变化
"result": "noop",
我们可以得出结论:
在更新操作时,URL带_update时,更新前会比对新老数据,如果我们新数据和旧数据完全相同,将不会进行任何操作,不会影响序列号、版本号信息。
如果URI不带_update,不会检查原数据
二、增加属性更新
增加属性更新和上方规则相同,直接添加属性都可以正常保存。下面仅以PUT为例:
访问地址:
PUT http://127.0.0.1:9200/customer/external/1
Body请求数据:
{
"name": "JSON Doe",
"age": 30
}
再次使用GET查询返回结果:
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 9,
"_seq_no": 9,
"_primary_term": 1,
"found": true,
"_source": {
"name": "JSON Doe",
"age": 30
}
}
三、删除数据信息
访问信息:
DELETE http://127.0.0.1:9200/customer/external/1
返回结果:
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 10,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 10,
"_primary_term": 1
}
尝试使用GET方法查询,返回没找到:
{
"_index": "customer",
"_type": "external",
"_id": "1",
"found": false
}
四、删除整个索引
访问信息:
DELETE http://127.0.0.1:9200/customer/
返回结果:
{
"acknowledged": true
}
尝试使用GET方法查询,返回404错误:
{
"error": {
"root_cause": [
{
"type": "index_not_found_exception",
"reason": "no such index [customer]",
"resource.type": "index_expression",
"resource.id": "customer",
"index_uuid": "_na_",
"index": "customer"
}
],
"type": "index_not_found_exception",
"reason": "no such index [customer]",
"resource.type": "index_expression",
"resource.id": "customer",
"index_uuid": "_na_",
"index": "customer"
},
"status": 404
}
五、bluk批量操作
由于POSTMan无法完成本功能,会出现400错误,出错原因是由于POSTMan自动优化了换行符号。
我们这时需要采用Kibana完成,官网就能下,可以搜一下教程几个命令安装很简单。
【案例一】
在左侧边栏(如果没有左上角有一个 ”三“ 点一下)点击 Dev Tool输入下面内容:
POST /customer/external/_bulk
{"index":{"_id":"1"}}
{"name": "JSON Doe"}
{"index":{"_id":"2"}}
{"name": "Jone Doe"}
返回结果:
#! Deprecation: [types removal] Specifying types in bulk requests is deprecated.
{
"took" : 753,
"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
}
}
]
}
【案例二】
Dev Tool输入下面内容:
POST /_bulk
{"delete": {"_index": "website", "_type": "blog", "_id": "123"}}
{"create": {"_index": "website", "_type": "blog", "_id": "123"}}
{"title": "My first blog post"}
返回结果:
#! Deprecation: [types removal] Specifying types in bulk requests is deprecated.
{
"took" : 940,
"errors" : false,
"items" : [
{
"delete" : {
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 1,
"result" : "not_found",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 404
}
},
{
"create" : {
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 2,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1,
"status" : 201
}
}
]
}
由此可见批量操作中的每一个操作相互独立,可以独立成功或失败,彼此没有影响。