Elasticsearch 初步检索
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号数据为
PUT customer/external/1
发送消息体
{ "name":"John Doe" }
返回消息体
{ "_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 的数据 就是新建 有就是更新
POST customer/external/
发送消息体
{ "name":"John Doe" }
返回消息体
{ "_index": "customer", "_type": "external", "_id": "1", "_version": 8, "_seq_no": 13, "_primary_term": 1, "found": true, "_source": { "name": "John Doe12" } }
二 如果用户A修改此数据带上
http://192.168.117.134:9200/customer/external/1?if_seq_no=13&if_primary_term=1
Body
//修改成功 { "_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
http://192.168.117.134:9200/customer/external/1?if_seq_no=13&if_primary_term=1
此时结果 报错
{ "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 并不支持这种操作)
http://192.168.117.134:9200/customer/external/1/_update
修改的消息体
{ "doc":{ "name":"John 大阿斯顿" } }
这里会与原数据做对比,如果要修改的数据和原数据一致,就不做操作,如果不一致就做更新操作
并且一定要带上
{ "doc":{ } }
6丶删除文档&索引(ES 没有提供删除类型的操作)
一 删除文档
DELETE customer/external/1 DELETE customer
直接发DELETE请求
http://192.168.117.134:9200/customer/external/1 DELETE
删除成功
{ "_index": "customer", "_type": "external", "_id": "1", "_version": 11, "result": "deleted", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 17, "_primary_term": 1 }
再次查询此id
{ "_index": "customer", "_type": "external", "_id": "1", "found": false }
二 删除索引
直接发DELETE请求
http://192.168.117.134:9200/customer/ DELETE
消息返回体
{ "acknowledged": true }
此时查询
{ "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
POST /customer/external/_bulk {"index":{"_id":"1"}} {"name":"John Doe"} {"index":{"_id":"2"}} {"name":"Jane Doe"}
复杂实例
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官方提供的测试数据
本文作者:KwFruit
本文链接:https://www.cnblogs.com/mangoubiubiu/p/16757204.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2020-10-06 上传头像开发-集成阿里云OSS