写在前面:
 继续记录自己的SpringBoot学习之旅,这次是SpringBoot应用相关知识学习记录。若看不懂则建议先看前几篇博客,详细代码可在我的Gitee仓库SpringBoot克隆下载学习使用!
3.4.3.4 ElasticSearch(ES)
3.4.3.4.1 介绍
- 是一个分布式全文搜索引擎
- 主要应用于需要搜索的功能里,比如商城
- 关键是倒排索引,根据关键词找到对应索引,再根据索引找到具体的数据
3.4.3.4.2 安装
- 点击下载,选择对应版本即可,这里是7.16.2
- 下载解压后点击elasticsearch.bat即可启动服务,如图![![[Pasted image 20220831113450.png]]](https://file.cfanz.cn/uploads/png/2022/11/24/1/96O5G557Gd.png) 
- 在浏览器输入地址localhost:9200,出现如图即可成功![![[Pasted image 20220831113528.png]]](https://file.cfanz.cn/uploads/png/2022/11/24/1/649281Q74U.png) 
3.4.3.4.3 操作
- 使用软件:postman或者Apifox
- 使用插件:IK分词器,点击下载解压后放在之前下好的ES中的插件目录plugins中
- 创建索引。添加put请求并指定分词规则,如图![![[Pasted image 20220831150028.png]]](https://file.cfanz.cn/uploads/png/2022/11/24/1/8V6C59B53E.png) 
 ,分词规则如下:
{
    "mappings": {
        "properties": {
            "id": {
                "type": "keyword"
            },
            "name": {
                "type": "text",
                "analyzer": "ik_max_word",
                "copy_to": "all"
            },
            "password": {
                "type": "text",
                "analyzer": "ik_max_word",
                "copy_to": "all"
            },
            "age": {
                "type": "keyword"
            },
            "all": {
                "type":"text",
                "analyzer":"ik_max_word"
            }
        }
    }
}
- 删除索引,同上,使用DELETE请求
- 查询索引,同上,使用GET请求,如图![![[Pasted image 20220831150227.png]]](https://file.cfanz.cn/uploads/png/2022/11/24/1/8bR63I8Se1.png) 
- 添加文档(数据),同上,使用POST请求,如图 
- 其中有3种方式: 
  - xxx/_ doc:id为默认
- xxx/ _ doc/2:指定id为2_
- xxx/_ create/3:指定id为3
 
- 查询文档全部数据,使用GET,如图![![[Pasted image 20220831153523.png]]](https://file.cfanz.cn/uploads/png/2022/11/24/1/D1d1BY6P9e.png) 
 ,查询单个则是xxx/ _ doc/id格式
- 删除文档数据,同上,用DELETE请求,如图![![[Pasted image 20220831154314.png]]](https://file.cfanz.cn/uploads/png/2022/11/24/1/348D1DDbJc.png) 
- 修改文档数据类似添加,主要有两种: 
  - 全量修改,直接覆盖掉原有的,如图![![[Pasted image 20220831154914.png]]](https://file.cfanz.cn/uploads/png/2022/11/24/1/4HW22P4Q06.png) 
- 部分修改,仅仅修改需要修改部分,如图![![[Pasted image 20220831162633.png]]](https://file.cfanz.cn/uploads/png/2022/11/24/1/7b11We0265.png) 
 
- 全量修改,直接覆盖掉原有的,如图
3.4.3.4.4 SpringBoot整合
- 新建项目
- 添加ES高版本坐标,如图![![[Pasted image 20220831174627.png]]](https://file.cfanz.cn/uploads/png/2022/11/24/1/65G18eS0fR.png) 
- 无需配置
- 客户端操作 
  - 创建索引
 
private RestHighLevelClient client;
 @Test  
    void createIndex() throws IOException {  
        HttpHost host = HttpHost.create("http://localhost:9200");;  
        RestClientBuilder builder = RestClient.builder(host);  
        client = new RestHighLevelClient(builder);  
//        客户端操作  
        CreateIndexRequest request = new CreateIndexRequest("users");  
        client.indices().create(request, RequestOptions.DEFAULT);  
//        关闭客户端  
        client.close();  
    }
- 添加文档
//    创建文档  
    @Test  
    public void addESDoc() throws IOException {  
        IndexRequest indexRequest = new IndexRequest("users").id("1");  
        String json = "{\n" +  
                "    \"name\": \"大家\",\n" +  
                "    \"password\": \"早上好大家\",\n" +  
                "    \"age\": 23\n" +  
                "}";  
        indexRequest.source(json,XContentType.JSON);  
        client.index(indexRequest,RequestOptions.DEFAULT);  
    }
其它具体详见Gitee上的项目









