• 使用Spring Data Elasticsearch 进行索引的增、删、改、查


    一 SpringBoot + Elasticsearch 项目环境搭建

    1.1 修改pom文件添加依赖

    目前使用spring-boot-starter-parent版本为2.2.8.RELEASE

    对应spring-data-elasticsearch版本为2.2.8.RELEASE,版本对应可以自行百度,如果不行直接用elasticsearch-rest-high-level-client工具类吧

          
                org.springframework.boot
                spring-boot-starter-data-elasticsearch
                2.2.8.RELEASE
            
    
            
                org.elasticsearch
                elasticsearch
                7.5.0
            
    
            
                org.elasticsearch.client
                elasticsearch-rest-client
                7.5.0
            
    
            
                org.elasticsearch.client
                elasticsearch-rest-high-level-client
                7.5.0
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    1.2 新建配置文件
    import org.apache.http.HttpHost;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * ES配置类
     *
     * @author lc
     * @version 1.0
     * @date 2022/3/25 10:53
     */
    @Configuration
    public class ElasticSearchClientConfig {
    
    
        @Bean
        public RestHighLevelClient restHighLevelClient() {
            RestHighLevelClient client = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("192.168.1.100", 9200, "http")));
            return client;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    RestHighLevelClient的使用

    RestHighLevelClient是Elasticsearch 的操作方法,我们先进行引用吧。

    @Autowired
    private RestHighLevelClient client;
    
    • 1
    • 2

    1、创建索引

     @Test
        void testCreateIndex() throws IOException {
            //1 创建索引请求
            CreateIndexRequest request = new CreateIndexRequest("zlc_index");
            //2 客户端执行请求
            CreateIndexResponse createIndexResponse =
                    client.indices().create(request, RequestOptions.DEFAULT);
    
            System.out.println(createIndexResponse);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2、索引是否存在

    @Test
        void testExistIndex() throws IOException {
            GetIndexRequest request = new GetIndexRequest("zlc_index");
            boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
            System.out.println(exists);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3、删除索引

    @Test
        void testDeleteIndex() throws IOException {
            DeleteIndexRequest request = new DeleteIndexRequest("zlc_index");
            AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
            System.out.println(delete.isAcknowledged());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    4、添加文档

    @Test
        void testAddDocument() throws IOException {
            //创建对象
            UserES user = new UserES();
            user.setUserName("suwerw");
            user.setUserPhone("178245774");
            //创建请求
            IndexRequest request = new IndexRequest("zlc_index");
            //规则 put /zlc_index/_doc/1
            request.id("1");
            request.timeout(TimeValue.timeValueSeconds(1));
            request.timeout("1s");
    
            //将数据放入请求
            request.source(JSON.toJSONString(user), XContentType.JSON);
    
            //客户端发送请求,获取响应结果
            IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
    
            System.out.println(indexResponse.toString());
            System.out.println(indexResponse.status());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    5、判断文档是否存在

    @Test
        void testIsExists() throws IOException {
            GetRequest getRequest = new GetRequest("zlc_index", "1");
            //不获取返回的 _source 的上下文,提高效率
            getRequest.fetchSourceContext(new FetchSourceContext(false));
            getRequest.storedFields("_none_");
    
            boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
            System.out.println(exists);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    6、获取文档

     @Test
        void testGetDocument() throws IOException {
            GetRequest getRequest = new GetRequest("zlc_index", "1");
            GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
            System.out.println(getResponse);
            System.out.println(getResponse.getSourceAsString());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    7、更新文档信息

     @Test
        void testUpdateDocument() throws IOException {
            UpdateRequest updateRequest = new UpdateRequest("zlc_index", "1");
            updateRequest.timeout("1s");
    
            UserES user = new UserES();
            user.setUserName("Zhou_LC");
            user.setUserPhone("233669");
            updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);
    
            UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
            System.out.println(updateResponse);
            System.out.println(updateResponse.status());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    8、删除文档

      @Test
        void testDeleteDocument() throws IOException {
            DeleteRequest deleteRequest = new DeleteRequest("zlc_index", "1");
            deleteRequest.timeout("1s");
    
            DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
            System.out.println(deleteResponse);
            System.out.println(deleteResponse.status());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    【性能测试】MySQL慢查询原因/排查思路+SQL优化与性能定位思路...
    【加油站会员管理小程序】01需求分析
    【Python实战】海量表情包炫酷来袭,快来pick斗图新姿势吧~(超好玩儿)
    参考线平滑-CostThetaSmoother-Ipopt
    爬虫学习日记第八篇(爬取fofa某端口的协议排行及其机器数目,统计top200协议)
    Windows wsl2安装Ubuntu
    LeetCode.H76.最小覆盖子串
    Swift之深入解析Sendable和@Sendable闭包代码实例
    SQL 基础入门教程
    【计算机网络】运输层习题(谢希仁)(1)
  • 原文地址:https://blog.csdn.net/weixin_43360488/article/details/133255795