57、elasticSearch基础入门安装 使用 IK分词器 Rest风格基本命令
一、下载使用elasticSearch,下载完成之后,解压就能用了
用docker下载并安装elasticSearch 下载: docker pull elasticsearch:7.8.0 运行: docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.8.0
用docker下载安装kibana 下载: docker pull kibana:7.8.0 运行: docker run -d --name kibana --link elasticsearch:elasticsearch -e "I18N_LOCALE=zh-CN" -p 5601:5601 kibana:7.8.0
1、解决跨域问题
http.cors.enabled: true http.cors.allow-origin: "*"
2、访问:localhost:9200 可以看到elasticSearch 运行成功
二、elasticsearch-head 可视化工具,是一个前端工程
安装:npm install
运行:npm run start
访问:localhost:9100 可以看到elasticSearch-head 运行成功
三、安装Kibana 可视化界面公共、下载Kibana 完成之后解压即可使用
汉化Kibana 修改配置文件改成:i18n.locale: "zh-CN"
访问:http://localhost:5601 可以看到Kibana 运行成功

四、ElasticSearch 概念
1、索引:(类似于:mysql的库)
2、类型:(类似于:mysql的表)
3、文档:(类似于:一条条的数据)
五、IK分词器的使用
1、下载:elasticsearch-analysis-ik-7.6.0.zip 解压这个压缩包,放到elasticSearch的插件包,重启ES服务器
2、使用Kibana进行测试: ik_smart 最小切分
GET _analyze { "analyzer":"ik_smart", "text": "哈哈" }
3、测试:ik_max_word 最细力度划分
GET _analyze { "analyzer":"ik_max_word", "text": "哈哈哈" }

六、自定义分词器

1、新建自己的分词的字典文件,然后在IK分词器的配置文件中配置指定的字典文件即可
2、再次测试
七、Rest风格操作(关于操作索引的操作)
1、创建索命令
PUT /test2/type1/1 { "name":"危存盛", "age":3, "gender": "男生" }

2、查看记录操作
GET test2
GET test2/type1/_search?q=name:危存盛
3、修改的操作
POST test2/type1/1/_update { "doc":{ "name":"危存盛111" } }
4、删除的操作
删除索引
DELETE test2
删除具体的文档
DELETE test1/user/1
八、关于操作文档的操作(重点)
1、创建数据
PUT kuangsheng/user/1 { "name":"张三", "age":22, "tags":["看书","撸代码","唱歌"] } PUT kuangsheng/user/2 { "name":"李四", "age":28, "tags":["玩游戏","撸代码","泡妞"] } PUT kuangsheng/user/3 { "name":"王五", "age":19, "tags":["登山","游泳","唱歌"] }
2、查询数据
简单的查询
GET /kuangsheng/user/1
3、修改操作
POST /kuangsheng/user/1/_update { "doc":{ "name":"张三111" } }
4、带条件的简单查询
GET /kuangsheng/user/_search?q=name:张三
九、复杂的条件查询(排序,分页,高亮,模糊查询,精准查询)
GET /kuangsheng/user/_search { "query": { "match": { "name": "李四" } }, "_source": ["name","tags"] } 2、排序查询 GET /kuangsheng/user/_search { "query": { "match": { "name": "李四" } }, "sort": [ { "age": { "order": "desc" } } ] }
2、分页查询
GET /kuangsheng/user/_search { "query": { "match": { "name": "李四" } }, "from": 0, "size": 10 }
3、多条件查询(相当于sql的and 条件)
GET /kuangsheng/user/_search { "query": { "bool": { "must": [ { "match": { "name": "李四" } }, { "match": { "age": 28 } } ] } } }
4、多条件查询(相当于sql的or 条件)
GET /kuangsheng/user/_search { "query": { "bool": { "should": [ { "match": { "name": "李四" } }, { "match": { "age": 28 } } ] } } }
5、范围查询
GET /kuangsheng/user/_search { "query": { "bool": { "filter": { "range": { "age": { "gte": 10, "lte": 25 } } } } } }
6、创建索引和字段的类型
PUT /testdb { "mappings": { "properties": { "name":{ "type": "text" }, "desc":{ "type": "keyword" } } } }
插入两条数据
PUT testdb/_doc/1 { "name":"狂神说Java", "desc":"狂神说Java name" } PUT testdb/_doc/2 { "name":"狂神说Java", "desc":"狂神说Java name2" }
GET /testdb/_search { "query": { "term": { "name":"狂" } } }
7、高亮查询
GET /testdb/_search { "query": { "term": { "name":"狂" } }, "highlight": { "pre_tags": "<P class='key' style='color:red' ", "post_tags": "/>", "fields": { "name":{} } } }
五、使用Postman 操作ElasticSearch
1、Postman 操作索引
创建一个edwin索引
PUT: http://localhost:9200/edwin
删除索引 DELETE: http://localhost:9200/edwin
查看单个索引 GET: http://localhost:9200/edwin
查看所有的索引 GET:http://localhost:9200/_cat/indices?v
2、Postman 操作文档(数据的增删改查)
使用PUT方式创建一个user文档的1号数据 PUT:http://localhost:9200/edwin/user/1 请求体 { "name":"张三", "age":22, "tags":["看书","撸代码","唱歌"] }
使用POST方式新增数据 POST:http://localhost:9200/edwin/user 请求体 { "name":"华为手机", "pirce":2000, "title":"小华为机正在热卖当中" }
根据主键ID查询 GET:http://localhost:9200/edwin/user/6
查询所有 GET:http://localhost:9200/edwin/user/_search
修改数据 POST:http://localhost:9200/edwin/_update/6 请求体 { "doc":{ "pirce":5000 } }
删除数据 DELETE:http://localhost:9200/edwin/_doc/6
3、Postman 操作文档(查询操作)
请求体匹配查询 GET http://localhost:9200/shopping/_search { "query":{ "match":{ "pirce":2000 } } }
查询所有 GET http://localhost:9200/shopping/_search { "query":{ "match_all":{ } } }
分页查询、展示指定数据、排序查询 GET http://localhost:9200/shopping/_search { "query":{ "match_all":{ } }, "from": 0, "size": 2, "_source":["name","title"], "sort":{ "pirce":{ "order":"desc" } } }
多条件查询 类似于 sql的 and GET http://localhost:9200/shopping/_search { "query":{ "bool":{ "must":[ { "match":{ "name":"小米手机" } }, { "match":{ "pirce":2000 } } ] } } }
多条件查询 类似于 sql的 OR GET http://localhost:9200/shopping/_search { "query":{ "bool":{ "should":[ { "match":{ "name":"小米手机" } }, { "match":{ "pirce":2000 } } ] } } }
范围查询 pirce 大于等于1000 的数据 GET http://localhost:9200/shopping/_search { "query":{ "bool":{ "filter":{ "range":{ "pirce":{ "gt": 1000 } } } } } }
完全匹配查询 match_phrase 查询高亮显示 GET http://localhost:9200/shopping/_search
{
"query":{
"match_phrase":{
"title": "小米"
}
},
"highlight":{
"fields":{
"name":{}
}
}
}
2、映射关系 type为text可以做模糊搜索,type为keyword只能精确匹配、index为true可以作为查询条件、index为false不能做查询
创建一个user索引,并设置映射关系 PUT http://localhost:9200/user/_mapping { "properties":{ "name": { "type":"text", "index": true }, "sex": { "type":"keyword", "index": true }, "tel": { "type":"keyword", "index": fales } } }

浙公网安备 33010602011771号