Elasticsearch 搜索引擎基本操作介绍(一)

2022-06-301190

● 本演示版本使用8.1.2 安装 ○ https://www.elastic.co/cn/downloads/elasticsearch ● 启动 ● 查看是否安装成功 ○ 问题一 :请求后没有返回结果 ○ 系统提示: received plaintext http traffic on an https channel, closing connection Netty4HttpChannel{ ○ 解决:https://blog.csdn.net/susu1083018911/article/details/122923633https://laoshininhao-app-test.oss-cn-wulanchabu.aliyuncs.com/images/2022/06/30/223f32d384604fc9b22ce02175f30b92.png ○ 启动成功 ○ https://laoshininhao-app-test.oss-cn-wulanchabu.aliyuncs.com/images/2022/06/30/1fc8e84512604a0983106462da8280b7.png ● 分词器 ○ 安装途径 ○ https://github.com/medcl/elasticsearch-analysis-ik/releases ○ 注意事项 ■ 安装的分词器需要匹配对应的 es 搜索引擎版本 ■ ik解压后的文件放在 plugins 目录下的一个目录中,否则就会闪退 ○ 安装成功 https://laoshininhao-app-test.oss-cn-wulanchabu.aliyuncs.com/images/2022/06/30/e1cd42b5012c4d7e842a1b2661acd5af.png 基本操作 索引 创建索引 ● Method:PUT ● index name 小写,不能有中文和中划线 操作 ● 127.0.0.1:9200/test_index 首次发送 { "acknowledged": true, "shards_acknowledged": true, "index": "test_index" } ● 127.0.0.1:9200/test_index 再次发送 ○ 表示索引已创建,本次创建失败 查询索引 ● Method:GET 操作 ● 127.0.0.1:9200/test_index 查看单个索引 { "test_index": { "aliases": {}, "mappings": {}, "settings": { "index": { "routing": { "allocation": { "include": { "_tier_preference": "data_content" } } }, "number_of_shards": "1", "provided_name": "test_index", "creation_date": "1656432667058", "number_of_replicas": "1", "uuid": "_3bCokEURZi1DoyhLpsdwQ", "version": { "created": "8010299" } } } } } ● 127.0.0.1:9200/_cat/indices?v 查看所有索引信息 删除索引 ● Method:DELETE 操作 ● 127.0.0.1:9200/test_index { "acknowledged": true } 文档 创建文档 ● Method:POST 127.0.0.1:9200/test_index/_doc ○ 非幂等性操作,每次发送文档信息,都会新建一个新的文档(体现为文档id都不一样,es 会随机生成一个文档id) ● Method:PUT/POST 127.0.0.1:9200/test_index/_doc/1 ○ 幂等性操作,每次发送文档信息都会设置一个自定义的文档id,后面发送的文档信息会覆盖前一次发送的文档内容 ○ 每创建一次相同id的文档,就会有自增的版本号 ■ 首次的结果为 created,二次发送的结果为updated 操作 ● 127.0.0.1:9200/test_index/_doc 首次发送 { "_index": "test_index", "_id": "efUsq4EBL9Az8w-9Ndvy", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 } ● 127.0.0.1:9200/test_index/_doc 首次发送 再次发送 { "_index": "test_index", "_id": "evUsq4EBL9Az8w-91NvP", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 1, "_primary_term": 1 } ● 127.0.0.1:9200/test_index/_doc/1 首次发送 { "_index": "test_index", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 2, "_primary_term": 1 } ● 127.0.0.1:9200/test_index/_doc/1 再次发送(相当于全覆盖) { "_index": "test_index", "_id": "1", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 3, "_primary_term": 1 } 查询 ● Method:GET ● 主键查询 ● 全查询 ○ 默认为10条数据 操作 ● 127.0.0.1:9200/test_index/_doc/1 主键查询 { "_index": "test_index", "_id": "1", "_version": 2, "_seq_no": 3, "_primary_term": 1, "found": true, "_source": { "title": "第一篇文章", "context": "浅浅尝试两次", "userId": 1, "createBy": "Falala", "createData": "20", "createTime": "2022-06-15T20:04:23.000Z" } } ● 127.0.0.1:9200/test_index/_search 全查询 ○ 查询增加分页 {"from":0,"size":999} { "took": 8, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 3, "relation": "eq" }, "max_score": 1.0, "hits": [ { "_index": "test_index", "_id": "efUsq4EBL9Az8w-9Ndvy", "_score": 1.0, "_source": { "title": "第一篇文章", "context": "浅浅尝试", "userId": 1, "createBy": "Falala", "createData": "20", "createTime": "2022-06-15T20:04:23.000Z" } }, { "_index": "test_index", "_id": "evUsq4EBL9Az8w-91NvP", "_score": 1.0, "_source": { "title": "第一篇文章", "context": "浅浅尝试", "userId": 1, "createBy": "Falala", "createData": "20", "createTime": "2022-06-15T20:04:23.000Z" } }, { "_index": "test_index", "_id": "1", "_score": 1.0, "_source": { "title": "第一篇文章", "context": "浅浅尝试两次", "userId": 1, "createBy": "Falala", "createData": "20", "createTime": "2022-06-15T20:04:23.000Z" } } ] } } 修改文档 ● Method: POST ● 完全覆盖:为创建指定id的再次发送 ● 部分覆盖 操作 ● 127.0.0.1:9200/test_index/_update/2 部分更新 { "doc": { "title":"第一篇修改后文章" } } { "_index": "test_index", "_id": "2", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 5, "_primary_term": 1 } 删除文档 ● Method:DELETE 操作 ● 127.0.0.1:9200/test_index/_doc/2 { "_index": "test_index", "_id": "2", "_version": 3, "result": "deleted", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 6, "_primary_term": 1 } 查询文档

● Method:Post 操作 ● 127.0.0.1:9200/test_index/_search 全量搜索 { "query": { // 全量出查询 "match_all": {} }, // 第一页获取两条数据 "from": 0, "size": 2, // 排序 "sort": { // 排序对象 "createTime": { // 顺序设定 "order": "desc" } } } { "took": 56, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 13, "relation": "eq" }, "max_score": null, "hits": [ { "_index": "test_index", "_id": "efUsq4EBL9Az8w-9Ndvy", "_score": null, "_source": { "title": "第一篇文章", "context": "浅浅尝试", "userId": 1, "createBy": "Falala", "createData": "20", "createTime": "2022-06-15T20:04:23.000Z" }, "sort": [ 1655323463000 ] }, { "_index": "test_index", "_id": "evUsq4EBL9Az8w-91NvP", "_score": null, "_source": { "title": "第一篇文章", "context": "浅浅尝试", "userId": 1, "createBy": "Falala", "createData": "20", "createTime": "2022-06-15T20:04:23.000Z" }, "sort": [ 1655323463000 ] } ] } } ● 127.0.0.1:9200/test_index/_search 全量搜索 + 获取置顶内容 { "query": { // 全量出查询 "match_all": {} }, // 第一页获取两条数据 "from": 0, "size": 2, // 指定获取查询内容 "_source": [ "title" ], // 排序 "sort": { // 排序对象 "createTime": { // 顺序设定 "order": "desc" } } } { "took": 5, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 13, "relation": "eq" }, "max_score": null, "hits": [ { "_index": "test_index", "_id": "efUsq4EBL9Az8w-9Ndvy", "_score": null, "_source": { "title": "第一篇文章" }, "sort": [ 1655323463000 ] }, { "_index": "test_index", "_id": "evUsq4EBL9Az8w-91NvP", "_score": null, "_source": { "title": "第一篇文章" }, "sort": [ 1655323463000 ] } ] } } ● 127.0.0.1:9200/test_index/_search 多条件搜索 { "query": { // 条件 "bool": { // 必须满足的条件 "must": [ { // 条件1 "match": { "title": "四" } }, { // 条件2 "match": { "createBy": "Falala" } } ] } } } { "took": 3, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 2.26996, "hits": [ { "_index": "test_index", "_id": "5G8ttIEBCnqd9vOBrXFh", "_score": 2.26996, "_source": { "title": "第四篇文章", "context": "再次浅浅尝试", "userId": 1, "createBy": "Falala", "createData": "20", "createTime": "2022-06-15T20:04:23.000Z" } } ] } }

分享
点赞0
打赏
上一篇:Docker常用命令笔记(一)
下一篇:Canal的使用