搜索引擎ES-RestHighLevelClient

本文介绍了如何在Java中引入并使用RestHighLevelClient与Elasticsearch进行交互,包括添加、批量添加、删除和查询数据的基本操作,同时也对比了ES与MySQL的关系,并涉及到了分页和排序的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

搜索引擎ES-RestHighLevelClient

前言:本篇文章,主要讲述如何引入RestHighLevelClient,以及对其API的调用,属于入门级别的接口实用。适用于由于工作需要及时了解的童靴们以及初学者。想要了解ES底层更多内容的童鞋们,本篇文章并不适合 !

前言:对比mysql,了解ES

首先通过图片对比我们了解的关系型数据库

在这里插入图片描述

一、引入依赖

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <!-- version版本需要根据项目版本定义 -->

二、pom配置

注意:除了使用pom配置外,也可以使用配置类就行详细配置。

spring:
  # 配置es
  elasticsearch:
    rest:
      uris: 127.0.0.1:9200
      username: ''
      password: ''

三、接口实操

  1. 添加数据到ES

    1. 	/**
           * 定义es的索引
           */
          private static final String ES_USER_INDEX = "aq_test";
      
      	/**
           * 插入数据到es
           */
          @Test
          public void insertTest() {
             	// 模拟数据
              UserInfo userInfo = new UserInfo(1l, "张无忌", "男", 25, new Date());
      
              try {
                  // 1、创建request对象,
                  IndexRequest indexRequest = new IndexRequest(ES_USER_INDEX);
                  // 1.1、手动放入id(这里的id可以类别关系型数据的主键),如果不设置,ES会自动生成不重复的id,删除查询等操作可以使用
                  indexRequest.id(userInfo.getId().toString());
                  
                  // 2、request放入source(可以理解为一个对象或一条数据)
                  indexRequest.source(gson.toJson(userInfo), XContentType.JSON);
                  
                  // 3、请求es,存放数据
                  IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
      
                  log.info("======= 保存用户到es成功,IndexResponse:{}", response);
                  // 日志内容(注意id字段,这里使ES自动生成的id):IndexResponse:IndexResponse[index=user,type=_doc,id=3DstoYYBO8T-GorTRO2o,version=1,result=created,seqNo=0,primaryTerm=1,shards={"total":2,"successful":1,"failed":0}]
              } catch (IOException e) {
                  log.error(e.getMessage(), e);
              }
          }
      
    2. 批量插入数据

      	/**
           * 批量保存数据到ES中
           */
          @Test
          public void bulkSaveTest() throws IOException {
      
              // 构建数据
              UserInfo user1 = new UserInfo(1l, "王源", "女", 12, DateUtil.parse("2000-08-09"));
              UserInfo user2 = new UserInfo(2l, "易烊千玺", "男", 34, DateUtil.parse("2001-08-10"));
              UserInfo user3 = new UserInfo(3l, "王俊凯", "女", 62, DateUtil.parse("1988-01-23"));
              UserInfo user4 = new UserInfo(4l, "诸葛亮", "女", 12, DateUtil.parse("2011-08-09"));
              UserInfo user5 = new UserInfo(5l, "张亮", "男", 34, DateUtil.parse("2001-05-10"));
              UserInfo user6 = new UserInfo(6l, "张三丰", "女", 62, DateUtil.parse("1888-01-23"));
              UserInfo user7 = new UserInfo(7l, "上官婉儿", "女", 12, DateUtil.parse("2020-08-09"));
              UserInfo user8 = new UserInfo(8l, "狄仁杰", "男", 34, DateUtil.parse("2001-08-10"));
              UserInfo user9 = new UserInfo(9l, "艾琳", "女", 62, DateUtil.parse("1988-12-23"));
      
              // 构建indexRequest
              IndexRequest request1 = new IndexRequest(ES_USER_INDEX, ES_USER_TYPE, user1.getId().toString());
              request1.source(gson.toJson(user1), XContentType.JSON);
      
              IndexRequest request2 = new IndexRequest(ES_USER_INDEX, ES_USER_TYPE, user2.getId().toString());
              request2.source(gson.toJson(user2), XContentType.JSON);
      
              IndexRequest request3 = new IndexRequest(ES_USER_INDEX, ES_USER_TYPE, user3.getId().toString());
              request3.source(gson.toJson(user3), XContentType.JSON);
      
              IndexRequest request4 = new IndexRequest(ES_USER_INDEX, ES_USER_TYPE, user4.getId().toString());
              request4.source(gson.toJson(user4), XContentType.JSON);
      
              IndexRequest request5 = new IndexRequest(ES_USER_INDEX, ES_USER_TYPE, user5.getId().toString());
              request5.source(gson.toJson(user5), XContentType.JSON);
      
              IndexRequest request6 = new IndexRequest(ES_USER_INDEX, ES_USER_TYPE, user6.getId().toString());
              request6.source(gson.toJson(user6), XContentType.JSON);
      
              IndexRequest request7 = new IndexRequest(ES_USER_INDEX, ES_USER_TYPE, user7.getId().toString());
              request7.source(gson.toJson(user7), XContentType.JSON);
      
              IndexRequest request8 = new IndexRequest(ES_USER_INDEX, ES_USER_TYPE, user8.getId().toString());
              request8.source(gson.toJson(user8), XContentType.JSON);
      
              IndexRequest request9 = new IndexRequest(ES_USER_INDEX, ES_USER_TYPE, user9.getId().toString());
              request9.source(gson.toJson(user9), XContentType.JSON);
      
              // 构建bulkrequest对象
              BulkRequest bulkRequest = new BulkRequest();
              bulkRequest.add(request2).add(request3).add(request4)
                      .add(request1).add(request5).add(request6).add(request7)
                      .add(request8).add(request9);
      
              // 批量保存
              BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
              log.info("========== 批量保存成功,response:{}", bulk);
      
          }
      
  2. 删除数据

    	/**
         * 删除
         */
        @Test
        public void deleteTest() throws IOException {
            // 1、DeleteRequest(String index, String id) 调用构造器构建对象,此时id派上用场
            DeleteRequest deleteRequest = new DeleteRequest(ES_USER_INDEX, "_NnfrIQBbL1dGMv_ESXr");
    
         	// 2、删除数据   
            DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
     		
            log.info("============= 删除es成功,DeleteResponse:{}", response);
        }
    
    	/**
         * 批量删除
         */
        @Test
        public void deleteTest() {
            // 批量删除
            DeleteRequest deleteRequest1 = new DeleteRequest(ES_USER_INDEX, "1");
            DeleteRequest deleteRequest2 = new DeleteRequest(ES_USER_INDEX, "2");
            DeleteRequest deleteRequest3 = new DeleteRequest(ES_USER_INDEX, "3");
            DeleteRequest deleteRequest4 = new DeleteRequest(ES_USER_INDEX, "4");
            DeleteRequest deleteRequest5 = new DeleteRequest(ES_USER_INDEX, "5");
            DeleteRequest deleteRequest6 = new DeleteRequest(ES_USER_INDEX, "6");
            DeleteRequest deleteRequest7 = new DeleteRequest(ES_USER_INDEX, "7");
            
            // 2、构建BulkRequest
            BulkRequest bulkRequest = new BulkRequest().add(deleteRequest1)
                    .add(deleteRequest2).add(deleteRequest3).add(deleteRequest4)
                    .add(deleteRequest6).add(deleteRequest5).add(deleteRequest7);
    
            try {
                // 3、调用bulk方法批量操作删除
                BulkResponse response = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
                log.info("============= 删除es成功,DeleteResponse:{}", response);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
  3. 查询数据

    	/**
         * 查询所有的数据
         * @throws IOException
         */
        @Test
        public void searchAllTest() throws IOException {
            // 1、创建 SearchRequest
            SearchRequest searchRequest = new SearchRequest(ES_USER_INDEX);
    
            // 2、创建SearchSourceBuilder
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    
            // 3、创建QueryBuilder
            MatchAllQueryBuilder queryBuilder = new MatchAllQueryBuilder();
            // 使用查询构建工具类来创建QueryBuilder
            // MatchAllQueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
    
            // 4、把QueryBuilder 放入 sourceBuilder中
            searchSourceBuilder.query(queryBuilder);
    
            // 5、把searchSourceBuilder放入searchRequest中
            searchRequest.source(searchSourceBuilder);
    
            // 6、查询
            SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
    
            // 7、结果解析
            SearchHits hits = search.getHits();
            List<UserInfo> result = Arrays.stream(hits.getHits()).map(hit -> {
                UserInfo userInfo = gson.fromJson(hit.getSourceAsString(), UserInfo.class);
                return userInfo;
            }).collect(Collectors.toList());
        }
    
    	/**
         * <h3>term与match 的区别:</h3>
         * <li>term:代表完全匹配,也就是精确查询,搜索前不会再对搜索词进行分词解析,直接对搜索词进行查找;</li>
         * <li>match:代表模糊匹配,搜索前会对搜索词进行分词解析,然后按分词匹配查找;</li>
         * <li>term主要用于精确搜索,match则主要用于模糊搜索;</li>
         * <li>term精确搜索相较match模糊查询而言,效率较高;</li>
         *
         * <h3>数据类型:text(缺省值)和keyword 的区别</h3>
         * <li>text:查询时会进行分词解析;</li>
         * <li>keyword:keyword类型的词不会被分词器进行解析,直接作为整体进行查询;</li>
         * @throws IOException
         */
        @Test
        public void searchTest2() throws IOException {
            SearchRequest searchRequest = new SearchRequest(ES_USER_INDEX);
    
            SearchSourceBuilder sourceBuilderByMatch = new SearchSourceBuilder().query(QueryBuilders.matchQuery("name", "王俊凯"));
            SearchSourceBuilder sourceBuilderByTerm = new SearchSourceBuilder().query(QueryBuilders.termQuery("name", "王"));
    
            /**
             * match查询
             */
            searchRequest.source(sourceBuilderByMatch);
            SearchResponse searchByMatch = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            // 遍历封装列表对象
            List<UserInfo> userListByMatch = Arrays.stream(searchByMatch.getHits().getHits()).map(searchHit -> {
                return JSON.parseObject(searchHit.getSourceAsString(), UserInfo.class);
            }).collect(Collectors.toList());
    
            /**
             * term 查询
             */
            searchRequest.source(sourceBuilderByTerm);
            SearchResponse searchByTerm = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            // 遍历封装列表对象
            List<UserInfo> userListByTerm = Arrays.stream(searchByTerm.getHits().getHits()).map(searchHit -> {
                return JSON.parseObject(searchHit.getSourceAsString(), UserInfo.class);
            }).collect(Collectors.toList());
    
        }
    
     	/**
         * <h3>分页 排序</h3>
         * <p>size 指定查询结果中返回指定条数。</p>
         * <p>from 用来指定起始返回位置,和size关键字连用可实现分页效果</p>
         * <p>深度分页的情况下,这种分页效率较低</p>
         * es目前支持最大的max_result_window = 10000,也就是from+size的大小不能超过10000
         * @throws IOException
         */
        @Test
        public void searchTestBySortPage() throws IOException {
            SearchRequest searchRequest = new SearchRequest(ES_USER_INDEX);
    
            SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
    
            query.sort("age", SortOrder.DESC);
    
            query.from(0);
            query.size(5);
    
            searchRequest.source(query);
            SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
    
            // 遍历封装列表对象
            List<UserInfo> userListByMatch = Arrays.stream(response.getHits().getHits()).map(searchHit -> {
                return JSON.parseObject(searchHit.getSourceAsString(), UserInfo.class);
            }).collect(Collectors.toList());
        }
    

FAULT);

       // 遍历封装列表对象
       List<UserInfo> userListByMatch = Arrays.stream(response.getHits().getHits()).map(searchHit -> {
           return JSON.parseObject(searchHit.getSourceAsString(), UserInfo.class);
       }).collect(Collectors.toList());
   }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值