深浅模式
初步检索
1、 _cat
GET /_cat/nodes: 查看所有节点 GET /_cat/health: 查看 es 健康状况 GET /_cat/master: 查看主节点 GET /_cat/indices: 查看所有索引 相当于show database 查看所有数据库信息
2、索引一个文档(保存)
一 PUT
保存一定要带id,如果es里没有该id 的数据 就是新建 有就是更新 保存一个数据,保存在哪个索引的哪个类型下,指定用哪个唯一标识 PUT customer/external/1; 在customer 索引(数据库的名字)下的external 类型下(表)保存1号数据为
shell
PUT customer/external/1发送消息体
json
{
"name":"John Doe"
}返回消息体
json
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}二 POST
保存时不指定id: 插入数据时会自动创建一个uuid 保存时指定id: 和put一样 如果es里没有该id 的数据 就是新建 有就是更新
shell
POST customer/external/发送消息体
json
{
"name":"John Doe"
}返回消息体
json
{
"_index": "customer",
"_type": "external",
"_id": "yH8WeHgB_91avzot0K17",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 5,
"_primary_term": 1
}3丶查询文档
shell
GET customer/external/1消息响应体
json
{
"_index": "customer", //哪个索引(数据库)
"_type": "external", //哪个类型 (表)
"_id": "1", //记录id
"_version": 2, //版本号
"_seq_no": 1, //并发控制字段,每次更新就会+1 ,用来做乐观锁
"_primary_term": 1, //同上,主分片重新分配。如重启,就会变化
"found": true,
"_source": { //真正的内容
"name": "John Doe"
}
}4丶乐观锁
json
更新携带 ?if_seq_no=0&if_primary_term=1一 当前id 为 1的数据是
json
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 8,
"_seq_no": 13,
"_primary_term": 1,
"found": true,
"_source": {
"name": "John Doe12"
}
}二 如果用户A修改此数据带上
json
http://192.168.117.134:9200/customer/external/1?if_seq_no=13&if_primary_term=1Body
json
//修改成功
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 9,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 14,
"_primary_term": 1
}
//此时查询 此时_seq_no 变为15
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 9,
"_seq_no": 14,
"_primary_term": 1,
"found": true,
"_source": {
"name": "John 大阿斯顿"
}
}三 在A用户修改完后的基础上,如果B用户再次修改 并且if_seq_no=13
json
http://192.168.117.134:9200/customer/external/1?if_seq_no=13&if_primary_term=1此时结果 报错
json
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, required seqNo [13], primary term [1]. current document has seqNo [14] and primary term [1]",
"index_uuid": "rZoYIImoSOqivOxK5j-hXw",
"shard": "0",
"index": "customer"
}
],
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, required seqNo [13], primary term [1]. current document has seqNo [14] and primary term [1]",
"index_uuid": "rZoYIImoSOqivOxK5j-hXw",
"shard": "0",
"index": "customer"
},
"status": 409
}5、更新文档
一 Post 带_update(Put 并不支持这种操作)
json
http://192.168.117.134:9200/customer/external/1/_update修改的消息体
json
{
"doc":{
"name":"John 大阿斯顿"
}
}这里会与原数据做对比,如果要修改的数据和原数据一致,就不做操作,如果不一致就做更新操作 并且一定要带上
json
{
"doc":{
}
}6、删除文档&索引(ES 没有提供删除类型的操作)
一 删除文档
json
DELETE customer/external/1
DELETE customer直接发DELETE请求
json
http://192.168.117.134:9200/customer/external/1 DELETE删除成功
json
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 11,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 17,
"_primary_term": 1
}再次查询此id
json
{
"_index": "customer",
"_type": "external",
"_id": "1",
"found": false
}二 删除索引
直接发DELETE请求
json
http://192.168.117.134:9200/customer/ DELETE消息返回体
json
{
"acknowledged": true
}此时查询
json
{
"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
}7、bulk 批量API
直接怼进去kibana 的dev tools
json
POST /customer/external/_bulk
{"index":{"_id":"1"}}
{"name":"John Doe"}
{"index":{"_id":"2"}}
{"name":"Jane Doe"}复杂实例
json
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","retry_on_conflict":3}}
{"doc" :{"title" : "My updated blog post"}}8、样本测试数据
es官方提供的测试数据https://raw.githubusercontent.com/elastic/elasticsearch/master/docs/src/test/resources/accounts.jsonhttps://github.com/elastic/elasticsearch/blob/7.5/docs/src/test/resources/accounts.json
json
POST /bank/account/_bulk
复制黏贴测试数据