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

打印 上一主题 下一主题

主题 505|帖子 505|积分 1515

目录

12.5 RestClient操作索引库
12.5.1创建库
12.5.2 删除索引库
12.5.3 判定是否存在
12.6 RestClient操作文档
12.6.1 新增文档
12.6.2 查询文档
12.6.3 修改文档
12.6.4 删除文档
12.6.5 批量导入文档


12.5 RestClient操作索引库

酒店mapping映射
  1.   
  2.   ​
  3.   PUT /hotel
  4.   {
  5.     "mappings": {
  6.       "properties": {
  7.         
  8.         "id": {
  9.           "type": "keyword",
  10.           "copy_to": "all"
  11.         },
  12.         
  13.         "name": {
  14.           "type": "text",
  15.           "analyzer": "ik_max_word"
  16.         },
  17.         
  18.         "address": {
  19.           "type": "keyword",
  20.           "index": false
  21.         },
  22.         
  23.         "price": {
  24.           "type": "integer"
  25.         },
  26.         
  27.         "score": {
  28.           "type": "integer"
  29.         },
  30.         
  31.         "brand": {
  32.           "type": "keyword",
  33.           "copy_to": "all"
  34.         },
  35.         
  36.         "city": {
  37.           "type": "keyword"
  38.         },
  39.         
  40.         "starName": {
  41.           "type": "keyword"
  42.         },
  43.         
  44.         "business": {
  45.           "type": "keyword"
  46.         },
  47.         
  48.         "location": {
  49.           "type": "geo_point"
  50.         },
  51.         
  52.         "pic": {
  53.           "type": "keyword",
  54.           "index": false
  55.         },
  56.         
  57.         "all": {
  58.           "type": "text",
  59.           "analyzer": "ik_max_word"
  60.         }
  61.         
  62.       }
  63.     }
  64.   }
复制代码
导入依赖
  1.   
  2.           <!--elasticSearch依赖-->
  3.           <dependency>
  4.               <groupId>org.elasticsearch.client</groupId>
  5.               <artifactId>elasticsearch-rest-high-level-client</artifactId>
  6.               <version>7.12.1</version>
  7.           </dependency>
复制代码

  1.   
  2.       <properties>
  3.           <java.version>1.8</java.version>
  4.           <elasticsearch.version>7.12.1</elasticsearch.version>
  5.       </properties>
复制代码

创建对象
  1.   
  2.   @SpringBootTest
  3.   class HotelIndexTest {
  4.       private RestHighLevelClient client;
  5.   ​
  6.       // 客户端初始化
  7.       @BeforeEach
  8.       void setUp(){
  9.           this.client = new RestHighLevelClient(RestClient.builder(
  10.                   HttpHost.create("http://192.168.142.129:9200")
  11.           ));
  12.       }
  13.   ​
  14.       @Test
  15.       void tetsInit(){
  16.           System.out.println(client);
  17.       }
  18.   ​
  19.       // 客户端销毁
  20.       @AfterEach
  21.       void tearDown() throws IOException {
  22.           this.client.close();
  23.       }
  24.       
  25.   }
复制代码
client.indices()包罗了操作索引库的全部方法
12.5.1创建库

  1.   
  2.       @Test
  3.       void testCreateHotelIndex() throws IOException {
  4.           // 1. 创建Request对象
  5.           CreateIndexRequest request = new CreateIndexRequest("hotel");
  6.           // 2. 准备请求的参数
  7.           request.source(MAPPING_TEMPLATE,XContentType.JSON);
  8.           // 3. 发送请求    client.indices()的返回值包含了索引库额所有操作
  9.           client.indices().create(request,RequestOptions.DEFAULT);
  10.       }
复制代码
MAPPING_TEMPLATE是自界说的常量,也就是上面创建索引库的语句

12.5.2 删除索引库

  1.   
  2.       @Test
  3.       void testDeleteIndex() throws IOException {
  4.           // 1. 创建request对象
  5.           DeleteIndexRequest request = new DeleteIndexRequest("hotel");
  6.           // 2. 发送请求
  7.           client.indices().delete(request,RequestOptions.DEFAULT);
  8.       }
复制代码
12.5.3 判定是否存在

  1.   
  2.       @Test
  3.       void testExistIndex() throws IOException {
  4.           // 1. 创建request对象
  5.           GetIndexRequest request = new GetIndexRequest("hotel");
  6.           // 2. 发送请求
  7.           boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
  8.           // 3. 输出
  9.           System.out.println(exists ? "索引库存在" : "索引库不存在");
  10.       }
复制代码
12.6 RestClient操作文档

12.6.1 新增文档

  1.   
  2.       @Test
  3.       void testIndexDocument() throws IOException {
  4.           // 在数据库查到数据
  5.           Hotel hotel = iHotelService.getById(61083L);
  6.           HotelDoc hotelDoc = new HotelDoc(hotel); // 经度 + 纬度 拼接之后的对象 ,即索引库需要的类型
  7.           // 1. 创建请求对象
  8.           IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
  9.           // 2. 准备json文档 把查到的对象转换成json对象
  10.           request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
  11.           // 3. 发送请求
  12.           client.index(request,RequestOptions.DEFAULT);
  13.       }
复制代码
12.6.2 查询文档

  1.   
  2.       @Test
  3.       void testGetDocumentById() throws IOException {
  4.           // 1. 准备request
  5.           GetRequest request = new GetRequest("hotel", "61083");
  6.           // 2. 发送请求
  7.           GetResponse response = client.get(request, RequestOptions.DEFAULT);
  8.           // 3. 从响应中解析对象
  9.           String json = response.getSourceAsString();
  10.           // 4. 把json转成HotelDoc对象
  11.           HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
  12.           System.out.println(hotelDoc);
  13.       }
复制代码
12.6.3 修改文档

第二种更新方式,即局部更新的代码 :
  1.   
  2.       @Test
  3.       void testUpdateDocumentById() throws IOException {
  4.           // 1. 准备Request
  5.           UpdateRequest request = new UpdateRequest("hotel", "61083");
  6.           // 2. 准备请求参数
  7.           request.doc(
  8.                   "age", 18,
  9.                   "name","Rose"
  10.           );
  11.           // 3. 发送请求
  12.           client.update(request,RequestOptions.DEFAULT);
  13.       }
复制代码
12.6.4 删除文档

  1.   
  2.       @Test
  3.       void testDeleteDocumentById() throws IOException {
  4.           // 1. 准备request
  5.           DeleteRequest request = new DeleteRequest("hotel","61083");
  6.           // 2. 发送请求
  7.           client.delete(request,RequestOptions.DEFAULT);
  8.       }
复制代码
12.6.5 批量导入文档

  1.   
  2.       @Test
  3.       void testBulk() throws IOException {
  4.           // 批量查询酒店数据
  5.           List<Hotel> hotels = iHotelService.list();
  6.           // 1. 创建Bulk请求
  7.           BulkRequest request = new BulkRequest();
  8.           // 2. 准备参数  添加多个新增的request
  9.           for (Hotel hotel : hotels) {
  10.               // 把hotel转成hotelDoc对象
  11.               HotelDoc hotelDoc = new HotelDoc(hotel);
  12.               request.add(
  13.                       new IndexRequest("hotel").id(hotelDoc.getId().toString())
  14.                               .source(JSON.toJSONString(hotelDoc),XContentType.JSON)
  15.               );
  16.           }
  17.   ​
  18.           // 3. 发送请求
  19.           client.bulk(request,RequestOptions.DEFAULT);
  20.       }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

渣渣兔

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表