ElactisSearch7
2021-05-09 15:45:15 17 举报
AI智能生成
es的基本使用以及api测试,跟完狂神的视频自己做了一个模仿京东的搜索页面(码云地址):https://gitee.com/xiaoyeya/es
作者其他创作
大纲/内容
初始es
es就是为了搜索而生的,它是基于lucene实现的
elasticsearch
安装
官网下载太慢了,建议使用华为云镜像下载
https://mirrors.huaweicloud.com/elasticsearch/?C=N&O=D
解压即可使用
进入bin目录,双击打开elasticsearch.bat开启
测试
启动成功后,浏览器输入localhost:9200,出现该情况就启动成功了
es可视化工具
安装
先安装nodejs才能使用
github下载可视化工具
https://codeload.github.com/mobz/elasticsearch-head/zip/refs/heads/master
测试
解压启动可视化工具
npm install
npm start<br>
访问9100端口发现存在跨域问题
解决跨域问题
修改es配置文件
添加两行配置
重启es,刷新9100<br>
kibana<br>
安装
下载kibana压缩包
https://mirrors.huaweicloud.com/kibana/7.6.1/kibana-7.6.1-windows-x86_64.zip
解压缩下载的压缩包
配置汉化
在config目录下修改kibana.config<br>
在最后一行添加i18n.locale: "zh-CN"
测试
启动es,再启动kibana,如果显示plugs is disable按回车
访问localhost:5601
ik分词器插件
安装
下载ik分词器
https://github-releases.githubusercontent.com/2993595/253ca300-6081-11ea-8025-36df3259471e?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20210508%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20210508T011736Z&X-Amz-Expires=300&X-Amz-Signature=b1054e4700975eb5c0dc8ed3089ed07bd4027b6d5db4345a33874bf80ac27f67&X-Amz-SignedHeaders=host&actor_id=62921757&key_id=0&repo_id=2993595&response-content-disposition=attachment%3B%20filename%3Delasticsearch-analysis-ik-7.6.1.zip&response-content-type=application%2Foctet-stream
解压到我们es的plugins目录下
重启es,观察插件ik分词器加载
测试
打开kibana,测试ik分词器
先看两个分词请求
发送GET方式的分词请求,分词器为ik_smart<br>
分词器ik_smart是最粗粒度划分
发送GET方式的分词请求,分词器为ik_max_word<br>
分词器ik_max_word是最细粒度划分
观察两个分词器的分词结果
我们最直观的感受就是smart分词器中所有字只出现一次,拼接起来就是我们的原话
而max_word分词器分词结果是出现了很多重复的词,这些词实际上都是我们字典中的词语
分词器分词需要算法和数据,我们的输入和分词器的字典都是数据,分词器根据我们的输入对比我们的字典,通过一定的算法逻辑得出我们的分词结果
也就是说我们只要更改分词器的字典就可以改变分词器的分词结果
自定义用户字典
准备工作
在ik分词插件的config目录下新建自定义字典,参考自带的字典,把自定义的词按行写入就行了<br>
更改配置文件,添加我们的自定义字典
打开配置文件
添加一行配置自定义字典的代码
重启es,kibana
我们再来看看ik_smart和ik_max_word的区别
加载自定义词典前
ik_smart
ik_max_word
加载自定义词典后
ik_smart
ik_max_word
对比前后可得知,smart分词器就是在字典中找到最接近的一组分词结果,如果存在完全相同的,那么结果就是本身,如果没有完全相同的再通过分词拆分得到分词结果,优先不分词,不行再分词,而max_word就是把所有结果给提供出来
Kibana通过RESTFUL风格操作数据
添加数据
修改数据
在kibana的开发工具中更新数据,更新操作要套一层"doc":{ ... }
观察可视化工具
查询数据
正常根据id唯一标识查寻
条件查询
先多加一条数据
再查询,只能查询出来张三
查年龄18的,张三李四都能查出来
删除数据
根据唯一标识删除数据
观察es可视化工具,查看结果,李四确实没了
集成SpringBoot实现java调用api
准备工作
新建一个空项目,专门存放我们的es代码模块
新建api测试module,配置项目环境为jdk1.8
初始化项目时导入es依赖
设置项目的es的pom依赖版本和本地一致
在properties的标签中添加<elasticsearch.version>7.6.1</elasticsearch.version>
配置es的配置类,注入到ioc容器中
新建ElasticSearchClientConfig类,增加@Configuration注解
注入bean
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2">package com.xiaoye.config;<br><br>import org.apache.http.HttpHost;<br>import org.elasticsearch.client.RestClient;<br>import org.elasticsearch.client.RestHighLevelClient;<br>import org.springframework.context.annotation.Bean;<br>import org.springframework.context.annotation.Configuration;<br><br>/**<br> * @author 小也<br> * @create 2021/5/8 18:50<br> */<br></font><b style=""><font color="#f384ae">@Configuration</font></b><font color="#cfd0d2"><br>public class ElasticSearchClientConfig {<br><br> </font><b style=""><font color="#f1753f">@Bean<br> public RestHighLevelClient restHighLevelClient(){<br> return new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));<br> }</font></b><font color="#cfd0d2"><br><br>}</font><br></pre>
测试es高级客户端的api
创建索引
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> @Test<br> void createIndex() throws IOException {<br> CreateIndexRequest request = new CreateIndexRequest("xiaoye");<br> CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);<br> System.out.println(response);<br> }</font><br></pre>
查看指定索引是否存在
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> @Test<br> void indexIsExist() throws IOException {<br> GetIndexRequest request = new GetIndexRequest("xiaoye");<br> boolean response = client.indices().exists(request, RequestOptions.DEFAULT);<br> System.out.println(response);<br> }</font><br></pre>
删除指定索引
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> @Test<br> void deleteIndex() throws IOException {<br> DeleteIndexRequest request = new DeleteIndexRequest("xiaoye");<br> AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);<br> System.out.println(response.isAcknowledged());<br> }</font><br></pre>
文档的CURD
导入fastjson依赖
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> <dependency><br> <groupId>com.alibaba</groupId><br> <artifactId>fastjson</artifactId> <br> <version>1.2.62</version><br> </dependency></font><br></pre>
先创建一个实体类
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2">package com.xiaoye.entity;<br><br>import lombok.AllArgsConstructor;<br>import lombok.Data;<br>import lombok.NoArgsConstructor;<br>import org.springframework.stereotype.Component; <br><br>/**<br> * @author 小也<br> * @create 2021/5/8 19:16<br> */<br>@Component // 注册到spring的容器中<br>@Data<br>@AllArgsConstructor<br>@NoArgsConstructor<br>public class User {<br> private String name;<br> private int age;<br>}</font><br></pre>
编写测试代码
添加记录
单一添加
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> @Test<br> void addData() throws IOException {<br> //创建对象<br> User user = new User("小也", 20);<br><br> //获取索引对象<br> IndexRequest request = new IndexRequest("xiaoye");<br><br> //设置记录唯一标识已经超时时间<br> request.id("1");<br> request.timeout("1s");<br><br></font><b style=""><font color="#fdb813"> //把我们的对象记录以JSON格式放入请求中,es是以json格式存储记录的<br> request.source(JSON.toJSONString(user), XContentType.JSON);</font></b><font color="#cfd0d2"><br><br> //发送请求,获得返回值<br> IndexResponse response = </font><b style=""><font color="#f384ae">client.index</font></b><b style=""><font color="#f384ae">(request, RequestOptions.DEFAULT);</font></b><font color="#cfd0d2"><br><br> System.out.println(response.status());<br> System.out.println(response.toString());<br> }</font><br></pre>
批量添加
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> @Test<br> void batchDate() throws IOException {<br></font><b style=""><font color="#f384ae"> //批量操作整合对象<br> BulkRequest request = new BulkRequest();</font></b><font color="#cfd0d2"><br> for (int i = 0; i < 10; i++) {<br> User user = new User("贾维斯" + i + "号", i);<br> request.add(new IndexRequest("xiaoye").id(String.valueOf(i)).source(JSON.toJSONString(user),XContentType.JSON));<br> }<br> BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);<br> //返回操作是否失败,返回false代表操作成功<br> System.out.println(response.hasFailures());<br> }</font><br></pre>
删除指定索引的记录
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> @Test<br> void deleteDate() throws IOException {<br> DeleteRequest request = new DeleteRequest("xiaoye", "1");<br> DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);<br> System.out.println(response);<br> }</font><br></pre>
修改指定索引指定id的记录
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> @Test<br> void updateDate() throws IOException {<br> UpdateRequest request = new UpdateRequest("xiaoye", "1");<br> User user = new User("xiaoye", 22);<br> request.doc(JSON.toJSONString(user),XContentType.JSON);<br> UpdateResponse response = client.update(request,RequestOptions.DEFAULT);<br> System.out.println(response.status());<br> }</font><br></pre>
查询指定索引的记录
简单查询
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> @Test<br> void getDate() throws IOException {<br> GetRequest request = new GetRequest("xiaoye", "1");<br> GetResponse response = client.get(request,RequestOptions.DEFAULT);<br> System.out.println(response.getSource().toString());<br> }</font><br></pre>
条件查询
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><font color="#cfd0d2"> @Test<br> void complexSearch() throws IOException {<br> SearchRequest index = new SearchRequest("xiaoye");<br><br> //构造整合者<br> SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();<br><br> //条件查询构造<br> MatchQueryBuilder matchAllQueryBuilder = QueryBuilders.matchQuery("name", "贾维斯");<br> searchSourceBuilder.query(matchAllQueryBuilder);<br>// TermQueryBuilder termQuery = QueryBuilders.termQuery("name", "贾维斯1号");<br>// searchSourceBuilder.query(termQuery);<br><br> //构造高亮,字段为name<br> HighlightBuilder highlightBuilder = new HighlightBuilder();<br> highlightBuilder.field("name");<br> searchSourceBuilder.highlighter(highlightBuilder);<br><br> //构造整合者放入请求中<br> index.source(searchSourceBuilder);<br><br> SearchResponse response = client.search(index, RequestOptions.DEFAULT);<br><br> System.out.println(JSON.toJSONString(response.getHits()));<br><br> System.out.println("===============================");<br><br> for (SearchHit hit : response.getHits().getHits()) {<br> System.out.println(hit.getSourceAsString());<br><br> // 打印出高亮片段<br> HighlightField highlightField = hit.getHighlightFields().get("name");<br> for (Text fragment : highlightField.getFragments()) {<br> System.out.println(fragment.toString());<br> }<br> }<br> }</font><br></pre>
0 条评论
下一页