山东大学软件学院项目实训-创新实训-基于大模型的旅游平台(三十)- 微服务(10)

目录

[12.5 RestClient操作索引库](#12.5 RestClient操作索引库)

12.5.1创建库

[12.5.2 删除索引库](#12.5.2 删除索引库)

[12.5.3 判断是否存在](#12.5.3 判断是否存在)

[12.6 RestClient操作文档](#12.6 RestClient操作文档)

[12.6.1 新增文档](#12.6.1 新增文档)

[12.6.2 查询文档](#12.6.2 查询文档)

[12.6.3 修改文档](#12.6.3 修改文档)

[12.6.4 删除文档](#12.6.4 删除文档)

[12.6.5 批量导入文档](#12.6.5 批量导入文档)


12.5 RestClient操作索引库

酒店mapping映射

复制代码
  
  ​
  PUT /hotel
  {
    "mappings": {
      "properties": {
        
        "id": {
          "type": "keyword",
          "copy_to": "all"
        },
        
        "name": {
          "type": "text",
          "analyzer": "ik_max_word"
        },
        
        "address": {
          "type": "keyword",
          "index": false
        },
        
        "price": {
          "type": "integer"
        },
        
        "score": {
          "type": "integer"
        },
        
        "brand": {
          "type": "keyword",
          "copy_to": "all"
        },
        
        "city": {
          "type": "keyword"
        },
        
        "starName": {
          "type": "keyword"
        },
        
        "business": {
          "type": "keyword"
        },
        
        "location": {
          "type": "geo_point"
        },
        
        "pic": {
          "type": "keyword",
          "index": false
        },
        
        "all": {
          "type": "text",
          "analyzer": "ik_max_word"
        }
        
      }
    }
  }

导入依赖

复制代码
  
          <!--elasticSearch依赖-->
          <dependency>
              <groupId>org.elasticsearch.client</groupId>
              <artifactId>elasticsearch-rest-high-level-client</artifactId>
              <version>7.12.1</version>
          </dependency>
复制代码
  
      <properties>
          <java.version>1.8</java.version>
          <elasticsearch.version>7.12.1</elasticsearch.version>
      </properties>

创建对象

复制代码
  
  @SpringBootTest
  class HotelIndexTest {
      private RestHighLevelClient client;
  ​
      // 客户端初始化
      @BeforeEach
      void setUp(){
          this.client = new RestHighLevelClient(RestClient.builder(
                  HttpHost.create("http://192.168.142.129:9200")
          ));
      }
  ​
      @Test
      void tetsInit(){
          System.out.println(client);
      }
  ​
      // 客户端销毁
      @AfterEach
      void tearDown() throws IOException {
          this.client.close();
      }
      
  }

client.indices()包含了操作索引库的所有方法

12.5.1创建库
复制代码
  
      @Test
      void testCreateHotelIndex() throws IOException {
          // 1. 创建Request对象
          CreateIndexRequest request = new CreateIndexRequest("hotel");
          // 2. 准备请求的参数
          request.source(MAPPING_TEMPLATE,XContentType.JSON);
          // 3. 发送请求    client.indices()的返回值包含了索引库额所有操作
          client.indices().create(request,RequestOptions.DEFAULT);
      }

MAPPING_TEMPLATE是自定义的常量,也就是上面创建索引库的语句

12.5.2 删除索引库
复制代码
  
      @Test
      void testDeleteIndex() throws IOException {
          // 1. 创建request对象
          DeleteIndexRequest request = new DeleteIndexRequest("hotel");
          // 2. 发送请求
          client.indices().delete(request,RequestOptions.DEFAULT);
      }
12.5.3 判断是否存在
复制代码
  
      @Test
      void testExistIndex() throws IOException {
          // 1. 创建request对象
          GetIndexRequest request = new GetIndexRequest("hotel");
          // 2. 发送请求
          boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
          // 3. 输出
          System.out.println(exists ? "索引库存在" : "索引库不存在");
      }

12.6 RestClient操作文档

12.6.1 新增文档
复制代码
  
      @Test
      void testIndexDocument() throws IOException {
          // 在数据库查到数据
          Hotel hotel = iHotelService.getById(61083L);
          HotelDoc hotelDoc = new HotelDoc(hotel); // 经度 + 纬度 拼接之后的对象 ,即索引库需要的类型
          // 1. 创建请求对象
          IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
          // 2. 准备json文档 把查到的对象转换成json对象
          request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
          // 3. 发送请求
          client.index(request,RequestOptions.DEFAULT);
      }
12.6.2 查询文档
复制代码
  
      @Test
      void testGetDocumentById() throws IOException {
          // 1. 准备request
          GetRequest request = new GetRequest("hotel", "61083");
          // 2. 发送请求
          GetResponse response = client.get(request, RequestOptions.DEFAULT);
          // 3. 从响应中解析对象
          String json = response.getSourceAsString();
          // 4. 把json转成HotelDoc对象
          HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
          System.out.println(hotelDoc);
      }
12.6.3 修改文档

第二种更新方式,即局部更新的代码 :

复制代码
  
      @Test
      void testUpdateDocumentById() throws IOException {
          // 1. 准备Request
          UpdateRequest request = new UpdateRequest("hotel", "61083");
          // 2. 准备请求参数
          request.doc(
                  "age", 18,
                  "name","Rose"
          );
          // 3. 发送请求
          client.update(request,RequestOptions.DEFAULT);
      }
12.6.4 删除文档
复制代码
  
      @Test
      void testDeleteDocumentById() throws IOException {
          // 1. 准备request
          DeleteRequest request = new DeleteRequest("hotel","61083");
          // 2. 发送请求
          client.delete(request,RequestOptions.DEFAULT);
      }
12.6.5 批量导入文档
复制代码
  
      @Test
      void testBulk() throws IOException {
          // 批量查询酒店数据
          List<Hotel> hotels = iHotelService.list();
          // 1. 创建Bulk请求
          BulkRequest request = new BulkRequest();
          // 2. 准备参数  添加多个新增的request
          for (Hotel hotel : hotels) {
              // 把hotel转成hotelDoc对象
              HotelDoc hotelDoc = new HotelDoc(hotel);
              request.add(
                      new IndexRequest("hotel").id(hotelDoc.getId().toString())
                              .source(JSON.toJSONString(hotelDoc),XContentType.JSON)
              );
          }
  ​
          // 3. 发送请求
          client.bulk(request,RequestOptions.DEFAULT);
      }
相关推荐
stark张宇1 天前
微服务架构必备:Gin + gRPC + Consul + Nacos + GORM 打造用户服务
微服务·gin·grpc
阿里云云原生5 天前
MSE Nacos Prompt 管理:让 AI Agent 的核心配置真正可治理
微服务·云原生
阿里云云原生5 天前
阿里云微服务引擎 MSE 及 API 网关 2026 年 1 月产品动态
微服务
麦聪聊数据5 天前
统一 Web SQL 平台如何收编企业内部的“野生数据看板”?
数据库·sql·低代码·微服务·架构
云司科技codebuddy6 天前
技术支持过硬Trae核心代理
大数据·运维·python·微服务
海兰6 天前
Jina Embeddings V5 Text + Elasticsearch 9.x 本地部署指南
elasticsearch·jenkins·jina
递归尽头是星辰6 天前
微服务事务分级治理:从 Seata 全模式到 TDSQL 实战
微服务·云原生·架构·分布式事务·事务分级治理
没有bug.的程序员6 天前
订单系统重构史诗:从单体巨兽到微服务矩阵的演进、数据一致性内核与分布式事务
java·微服务·矩阵·重构·分布式事务·数据一致性·订单系统
江西理工大学小杨6 天前
高性能 C++ 社交平台4:基于 Boost.Beast 的 WebSocket 网关实现
c++·websocket·微服务
luod6 天前
Docker 快速安装Jenkins
java·docker·jenkins