• SpringBoot整合RestHighLevelClient实现索引操作以及文档操作


      😊 @ 作者: 一恍过去
      🎊 @ 社区: Java技术栈交流
      🎉 @ 主题: SpringBoot整合RestHighLevelClient实现索引操作以及文档操作
      ⏱️ @ 创作时间: 2022年08月13日

      1、pom引入

      <dependencies>
              <dependency>
                  <groupId>org.springframework.bootgroupId>
                  <artifactId>spring-boot-starter-webartifactId>
              dependency>
      
              <dependency>
                  <groupId>org.projectlombokgroupId>
                  <artifactId>lombokartifactId>
                  <optional>trueoptional>
              dependency>
              <dependency>
                  <groupId>org.springframework.bootgroupId>
                  <artifactId>spring-boot-starter-testartifactId>
                  <scope>testscope>
              dependency>
              <dependency>
                  <groupId>commons-iogroupId>
                  <artifactId>commons-ioartifactId>
                  <version>2.7version>
              dependency>	
      		<dependency>
                  <groupId>org.elasticsearchgroupId>
                  <artifactId>elasticsearchartifactId>
                  <version>7.8.0version>
              dependency>
              
              <dependency>
                  <groupId>org.elasticsearch.clientgroupId>
                  <artifactId>elasticsearch-rest-high-level-clientartifactId>
                  <version>7.8.0version>
              dependency>
              
              <dependency>
                  <groupId>org.apache.logging.log4jgroupId>
                  <artifactId>log4j-apiartifactId>
                  <version>2.8.2version>
              dependency>
          dependencies>
      
      • 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
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39

      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;
      
      /**
       * @Author: 
       * @Date: 2022/8/13 10:47
       * @Description:
       **/
      @Configuration
      public class ElasticsearchConfig {
      
          @Bean
          public RestHighLevelClient restHighLevelClient() {
              return new RestHighLevelClient(
                      // 配置ES连接地址
                      RestClient.builder(new HttpHost("192.168.80.121", 9200, "http"))
              );
          }
      }
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23

      3、索引操作

      3.1 创建索引

      
      @RestController
      @RequestMapping("/index")
      @Slf4j
      public class IndexController {
      
          @Resource
          private RestHighLevelClient restHighLevelClient;
      
          /**
           * 创建索引
           * @return
           * @throws IOException
           */
          @PostMapping("/create")
          public Object test() throws IOException {
              // 创建索引,索引名称为nba
              CreateIndexRequest request = new CreateIndexRequest("player");
              //设置分片和副本数
              request.settings(Settings.builder()
                      .put("index.number_of_shards", 3)
                      .put("index.number_of_replicas", 2)
              );
              IndicesClient indices = restHighLevelClient.indices();
              CreateIndexResponse response = indices.create(request, RequestOptions.DEFAULT);
              return response;
          }
      }
      
      • 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
      • 27
      • 28

      响应数据:
      在这里插入图片描述

      3.2 查询索引

      
      @RestController
      @RequestMapping("/index")
      @Slf4j
      public class IndexController {
      
          @Resource
          private RestHighLevelClient restHighLevelClient;
      
          /**
           * 查询索引
           * @return
           * @throws IOException
           */
          @GetMapping("/select")
          public Object select() throws IOException {
              // 查询索引,索引名称为 player
              GetIndexRequest request = new GetIndexRequest("player");
              IndicesClient indices = restHighLevelClient.indices();
              GetIndexResponse response = indices.get(request, RequestOptions.DEFAULT);
      
              return response;
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24

      3.3 删除索引

      
      @RestController
      @RequestMapping("/index")
      @Slf4j
      public class IndexController {
      
          @Resource
          private RestHighLevelClient restHighLevelClient;
      
          /**
           * 删除索引
           * @return
           * @throws IOException
           */
          @DeleteMapping("/delete")
          public Object delete() throws IOException {
              // 删除索引,索引名称为 player
              DeleteIndexRequest request = new DeleteIndexRequest("player");
              IndicesClient indices = restHighLevelClient.indices();
              AcknowledgedResponse response = indices.delete(request, RequestOptions.DEFAULT);
      
              return response;
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24

      响应数据:
      在这里插入图片描述

      3.4 判断索引是否存在

      
      @RestController
      @RequestMapping("/index")
      @Slf4j
      public class IndexController {
      
          @Resource
          private RestHighLevelClient restHighLevelClient;
      
          /**
           * 判断索引是否存在
           * @return
           * @throws IOException
           */
          @GetMapping("/exists")
          public Object exists() throws IOException {
              // 判断索引是否存在,索引名称为 player
              GetIndexRequest request = new GetIndexRequest("player");
              IndicesClient indices = restHighLevelClient.indices();
              boolean exists = indices.exists(request, RequestOptions.DEFAULT);
      
              return exists;
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24

      3.5 创建时进行映射

      为了方便索引数据操作,简单封装映射配置工具类,MappingUtil

      MappingUtil:

      import java.util.HashMap;
      import java.util.Map;
      
      /**
       * @Author: 
       * @Date: 2022/8/13 16:01
       * @Description:
       **/
      public class MappingUtil {
      
          /**
           * @param type     字段类型
           * @param analyzer 分词器
           * @param index    是否索引,默认true
           * @param store    是否储存, 默认false
           */
          public static Map<String, String> generateMapping(String type, String analyzer, String index, String store) {
              Map<String, String> valueMap = new HashMap<>(8);
              valueMap.put("type", type);
              if(analyzer!=null) {
                  valueMap.put("analyzer", analyzer);
              }
              if(index!=null) {
                  valueMap.put("index", index);
              }
              if(store!=null) {
                  valueMap.put("store", store);
              }
              return valueMap;
          }
      }
      
      • 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
      • 27
      • 28
      • 29
      • 30
      • 31

      创建索引的格式为:

      {
          "properties":{
              "name":{
                  "type":"text",
                  "analyzer":"ik_max_word"
              },
              "address":{
                  "type":"keyword",
                  "index":"false"
              },
              "age":{
                  "type":"integer",
                  "index":"false"
              }
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16

      创建索引的代码为:

      @RestController
      @RequestMapping("/index")
      @Slf4j
      public class IndexController {
      
          @Resource
          private RestHighLevelClient restHighLevelClient;
      
          /**
           * 创建索引同时创建映射
           * @return
           * @throws IOException
           */
          @PostMapping("/createMapping")
          public Object createMapping() throws IOException {
              // 创建索引,索引名称为 player_test
              CreateIndexRequest request = new CreateIndexRequest("player_test");
              //设置分片和副本数
              request.settings(Settings.builder()
                      .put("index.number_of_shards", 3)
                      .put("index.number_of_replicas", 2)
              );
      
              // 构建索引
              Map<String,Object> map = new HashMap<>(8);
              map.put("name",MappingUtil.generateMapping("text","ik_max_word",null,null));
              map.put("address",MappingUtil.generateMapping("keyword",null,"false",null));
              map.put("age",MappingUtil.generateMapping("integer",null,"false",null));
              Map<String,Object> properties = new HashMap<>();
              properties.put("properties",map);
      
      		// 设置索引
              request.mapping(properties);
              IndicesClient indices = restHighLevelClient.indices();
              CreateIndexResponse response = indices.create(request, RequestOptions.DEFAULT);
              return response;
          }
      }
      
      • 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
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38

      4、文档操作

      4.1 新增文档

      往已存在的索引player中新增文档

      新建实体类:

      @Data
      public class Player {
          /**
           * id
           */
          String id;
      
          /**
           * 名称
           */
          String name;
          /**
           * 地址
           */
          String address;
          /**
           * 年龄
           */
          Integer age;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20

      新增文档:

      /**
       * @Author: 
       * @Description:
       **/
      @RestController
      @RequestMapping("/doc")
      @Slf4j
      public class DocController {
      
          @Resource
          private RestHighLevelClient restHighLevelClient;
      
          /**
           * 新增文档
           * @return
           * @throws IOException
           */
          @PostMapping("/add")
          public Object add() throws IOException {
      
              // 创建对象
              String id = String.valueOf(new Random().nextInt(999999999));
              log.info("新增ID:"+id);
              Player player = new Player();
              player.setId(id);
              player.setName("李四");
              player.setAddress("东城区");
              player.setAge(20);
      
              // 创建添加文档的索引对象,索引必须先存在,索引名称为 player
              IndexRequest request = new IndexRequest().index("player");
              // 设置ID,可以不设置,不设置时es默认创建
              request.id(player.getId());
      
              // 添加文档数据
              request.source(JSON.toJSONString(player),XContentType.JSON);
      
              // 请求es
              IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
              return response;
          }
      }
      
      • 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
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42

      响应数据:
      在这里插入图片描述

      4.2 批量新增文档

      /**
       * @Author: 
       * @Description:
       **/
      @RestController
      @RequestMapping("/doc")
      @Slf4j
      public class DocController {
      
          @Resource
          private RestHighLevelClient restHighLevelClient;
      
          /**
           * 批量新增文档
           * @return
           * @throws IOException
           */
          @PostMapping("/addBatch")
          public Object addBatch() throws IOException {
      
              // 批量插入数据
              BulkRequest request = new BulkRequest();
      
              for(int a =0;a<=10;a++) {
                  // 创建对象
                  String id = String.valueOf(new Random().nextInt(999999999));
                  Player player = new Player();
                  player.setId(id);
                  player.setName("李四");
                  player.setAddress("东城区");
                  player.setAge(20);
      
                  // 添加文档数据
                  IndexRequest source = new IndexRequest().index("player").id(player.getId());
                  source.source(JSON.toJSONString(player), XContentType.JSON);
      
                  request.add(source);
              }
      
              // 请求es
              BulkResponse response = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
              return response;
          }
      }
      
      • 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
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44

      4.3 修改文档

      /**
       * @Author: 
       * @Description:
       **/
      @RestController
      @RequestMapping("/doc")
      @Slf4j
      public class DocController {
      
          @Resource
          private RestHighLevelClient restHighLevelClient;
      
          /**
           * 修改文档
           * @return
           * @throws IOException
           */
          @PutMapping("/update")
          public Object update() throws IOException {
      
              // 创建对象
              Player player = new Player();
              player.setName("张三");
              player.setAddress("东城区");
      
              // 修改文档时索引必须先存在,索引名称为 player
              UpdateRequest request = new UpdateRequest().index("player");
              // 设置修改文档对象的ID,文档ID必须存在才能被修改
              request.id("163625384");
      
              // 添加文档数据
              request.doc(JSON.toJSONString(player),XContentType.JSON);
      
              // 请求es
              UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
              return response;
          }
      }
      
      • 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
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38

      响应数据:
      在这里插入图片描述

      4.4 查看文档

      /**
       * @Author: 
       * @Description:
       **/
      @RestController
      @RequestMapping("/doc")
      @Slf4j
      public class DocController {
      
          @Resource
          private RestHighLevelClient restHighLevelClient;
      
          /**
           * 查看文档
           * @return
           * @throws IOException
           */
          @GetMapping("/select")
          public Object select() throws IOException {
      
              GetRequest request = new GetRequest();
              // 查看文档的id
              request.index("player").id("163625384");
      
              GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
              return response;
          }
      }
      
      • 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
      • 27
      • 28

      响应数据:
      在这里插入图片描述

      4.5 判断文档是否存在

      /**
       * @Author: 
       * @Description:
       **/
      @RestController
      @RequestMapping("/doc")
      @Slf4j
      public class DocController {
      
          @Resource
          private RestHighLevelClient restHighLevelClient;
      
          /**
           * 判断文档是否存在
           * @return
           * @throws IOException
           */
          @GetMapping("/exists")
          public Object exists() throws IOException {
      
              GetRequest request = new GetRequest();
              // 查看文档的id
              request.index("player").id("163625384");
      
              boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
              return exists;
          }
      }
      
      • 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
      • 27
      • 28

      4.6 删除文档

      /**
       * @Author: 
       * @Description:
       **/
      @RestController
      @RequestMapping("/doc")
      @Slf4j
      public class DocController {
      
          @Resource
          private RestHighLevelClient restHighLevelClient;
      
          /**
           * 删除文档
           * @return
           * @throws IOException
           */
          @DeleteMapping("/delete")
          public Object delete() throws IOException {
      
              DeleteRequest request = new DeleteRequest();
              // 删除文档的id,id必须存在
              request.index("player").id("948954987");
      
              DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
              return response;
          }
      }
      
      • 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
      • 27
      • 28

      响应参数:
      在这里插入图片描述

      4.7 批量删除文档

      /**
       * @Author: 
       * @Description:
       **/
      @RestController
      @RequestMapping("/doc")
      @Slf4j
      public class DocController {
      
          @Resource
          private RestHighLevelClient restHighLevelClient;
      
          /**
           * 批量删除文档
           * @return
           * @throws IOException
           */
          @ApiOperation(value = "批量删除文档", notes = "批量删除文档")
          @ApiOperationSupport(order = 5)
          @DeleteMapping("/deleteBatch")
          public Object deleteBatch() throws IOException {
      
              // 批量删除数据
              BulkRequest request = new BulkRequest();
      
              DeleteRequest request2 = new DeleteRequest().index("player").id("163625384");
              DeleteRequest request3 = new DeleteRequest().index("player").id("163625385");
              DeleteRequest request4 = new DeleteRequest().index("player").id("163625386");
      
              request.add(request2);
              request.add(request3);
              request.add(request4);
      
              BulkResponse response = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
              return response;
          }
      }
      
      • 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
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
    • 相关阅读:
      【OpenCV实现图像:OpenCV进行OCR字符分割】
      技术应用:C# System.Data.DataTable().Compute 基本用法
      【综合笔试题】难度 3.5/5,多解法热门二叉树笔试题
      集合论
      前端应用聚合整改
      马斯克拟打造xAI“算力超级工厂”,助力聊天机器人Grok
      银行卡号识别
      window环境导入odbc数据源
      Java技能树-RE-DOTALL/MULTILINE
      Spring cloud day(8) stream
    • 原文地址:https://blog.csdn.net/zhuocailing3390/article/details/126318849