一. 节点状况_cat
- Get /_cat/nodes: 查看所有节点
- Get /_cat/health: 查看es健康状况
- Get /_cat/master: 查看主节点
- Get /_cat/indices: 查看所有索引 类似与sql的show databases
二. 查询文档
- GET customer/external/1
响应结果
{
"_index": "customer", // 在哪个索引
"_type": "external", // 在哪个类型
"_id": "1", // 记录id
"_version": 2, // 版本号
"_seq_no": 1, //并发控制字段,每次更新就会+1,用来做乐观锁
"_primary_term": 1, // 同上,主分片重新分配,如重启,就会变化
"found": true,
"_source": { //真正的内容
"name": "test"
}
}
- _seq_no和_primary_ter乐观锁的使用
postman中测试:
PUT http://localhost:9200/customer/external/1?if_seq_no=7&if_primary_term=1
只有_seq_no和_primary_term是最新值时,才能更新成功,如果不是,则返回409错误:
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, required seqNo [7], primary term [1]. current document has seqNo [8] and primary term [1]",
"index_uuid": "BVsVjaw_Rzaxkhu6LGd4-A",
"shard": "0",
"index": "customer"
}
],
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, required seqNo [7], primary term [1]. current document has seqNo [8] and primary term [1]",
"index_uuid": "BVsVjaw_Rzaxkhu6LGd4-A",
"shard": "0",
"index": "customer"
},
"status": 409
}
三. 更新文档
- post更新 带_update
http://localhost:9200/customer/external/1/_update
注意在用_update时,必须要使用"doc"
请求示例
{
"doc": {
"name": "test111"
}
}
这种方式会将传入数据和原来的存储数据做比较,如果一样,则不执行更新,_version、_seq_no、_primary_term字段也不会发生变化
- post更新 不带_update
http://localhost:9200/customer/external/1
注意在不用_update时,不要携带"doc"
请求示例
{
"name":"123"
}
这种方式不会将传入数据和原来的存储数据做比较,每次都会更新
- put更新 不能携带_update
http://localhost:9200/customer/external/1
请求示例:
{
"name": "qus",
"age": "男"
}
这种方式不会将传入数据和原来的存储数据做比较,每次都会更新
四. 删除
- 删除文档:DELETE /customer/external/1
postman发送请求示例: DELETE http://localhost:9200/customer/external/1 - 删除索引:DELETE /customer
postman发送请求示例: DELETE http://localhost:9200/customer
五. 批量bulk
- 批量操作指定索引
语法格式:
POST /index/type/_bulk
{action:{metadata}}
{requestBody}
{action:{metadata}}
{requestBody}
...
(1). action(行为):
create:文档不存在时创建
update:更新文档
index:创建新文档或替换已有文档
delete:删除一个文档
(create和index的区别:如果文档存在,使用create会失败,会提示文档已经存在;但使用index则可以成功执行,将替换已有文档)
(2). metadata:_index,_type,_id
示例:
POST /customer/external/_bulk
{"index":{"_id":"1"}}
{"name":"qus2"}
{"index":{"_id":"2"}}
{"name":"qus3"}
- 批量操作不同的索引,不同的文档
复杂示例:
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"}}
{"title":"My second blog post"}
{"update":{"_index":"website","_type":"blog","_id":"123"}}
{"doc":{"title":"My update blog post"}}
- 导入测试样本数据
主要提供后面练习
测试样本数据地址:
https://raw.githubusercontent.com/elastic/elasticsearch/master/docs/src/test/resources/accounts.json
POST /bank/account/_bulk
【此处贴入上述地址中的所有数据】