• ElasticSearch是什么?ElasticSearch在SpringBoot中怎么用?SpringBoot整合ElasticSearch


    写在前面
    继续记录自己的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]]
    • 在浏览器输入地址localhost:9200,出现如图即可成功![[Pasted image 20220831113528.png]]
    3.4.3.4.3 操作
    • 使用软件:postman或者Apifox
    • 使用插件:IK分词器,点击下载解压后放在之前下好的ES中的插件目录plugins中
    • 创建索引。添加put请求并指定分词规则,如图![[Pasted image 20220831150028.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"
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 删除索引,同上,使用DELETE请求
    • 查询索引,同上,使用GET请求,如图![[Pasted image 20220831150227.png]]
    • 添加文档(数据),同上,使用POST请求,如图在这里插入图片描述
    • 其中有3种方式:
      • xxx/_ doc:id为默认
      • xxx/ _ doc/2:指定id为2_
      • xxx/_ create/3:指定id为3
    • 查询文档全部数据,使用GET,如图![[Pasted image 20220831153523.png]]
      ,查询单个则是xxx/ _ doc/id格式
    • 删除文档数据,同上,用DELETE请求,如图![[Pasted image 20220831154314.png]]
    • 修改文档数据类似添加,主要有两种:
      • 全量修改,直接覆盖掉原有的,如图![[Pasted image 20220831154914.png]]
      • 部分修改,仅仅修改需要修改部分,如图![[Pasted image 20220831162633.png]]
    3.4.3.4.4 SpringBoot整合
    • 新建项目
    • 添加ES高版本坐标,如图![[Pasted image 20220831174627.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();  
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 添加文档
    //    创建文档  
        @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);  
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    其它具体详见Gitee上的项目

  • 相关阅读:
    2023.10.07
    MySQL数据类型
    sn-38,7-乙基-10-羟基喜树碱,CAS:86639-52-3
    7.7 实现进程内存读写
    Unity 下载Zip压缩文件并且解压缩
    【无标题】
    80C51单片机的七种寻址方式
    深度学习知识点:循环神经网络(RNN)、长短期记忆网络(LSTM)、门控循环单元(GRU)
    Java枚举类 (详细解析java中的枚举类深入浅出)
    pl/sql之各参数详解(“箱子模型“)
  • 原文地址:https://blog.csdn.net/qq_40903378/article/details/127951010