SpringCloud:ElasticSearch之RestClient操作文档

为了与索引库操作分离,我们再次加一个测试类,做两件事情:

  • 初始化RestHighLevelClient
  • 我们的酒店数据在数据库,需要利用IHotelService去查询,所以注入这个接口
package com.dcxuexi.hotel;

import com.dcxuexi.hotel.service.IHotelService;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

/***
 * @Title HotelDocumentTest
 * @Description TOTD
 * @Auter DongChuang
 * @Date 2023/4/3 19:44
 * @Version 1.0.0
 */
@SpringBootTest
public class HotelDocumentTest {
    
    
    @Autowired
    private IHotelService HotelService;
    private RestHighLevelClient client;

    @BeforeEach
    void setup(){
    
    
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("https:192.168.1.111:9200")
        ));
    }

    @AfterEach
    void  tearDown() throws IOException {
    
    
        this.client.close();
    }

}

1.新增文档

我们要将数据库的酒店数据查询出来,写入elasticsearch中。

1.1.索引库实体类

数据库查询后的结果是一个Hotel类型的对象。结构如下:

package com.dcxuexi.hotel.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("tb_hotel")
public class Hotel {
    
    
    @TableId(type = IdType.INPUT)
    private Long id;
    private String name;
    private String address;
    private Integer price;
    private Integer score;
    private String brand;
    private String city;
    private String starName;
    private String business;
    private String longitude;
    private String latitude;
    private String pic;
}

与我们的索引库结构存在差异:

  • longitudelatitude需要合并为location

因此,我们需要定义一个新的类型,与索引库结构吻合:

package com.dcxuexi.hotel.pojo;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class HotelDoc {
    
    
    private Long id;
    private String name;
    private String address;
    private Integer price;
    private Integer score;
    private String brand;
    private String city;
    private String starName;
    private String business;
    private String location;
    private String pic;

    public HotelDoc(Hotel hotel) {
    
    
        this.id = hotel.getId();
        this.name = hotel.getName();
        this.address = hotel.getAddress();
        this.price = hotel.getPrice();
        this.score = hotel.getScore();
        this.brand = hotel.getBrand();
        this.city = hotel.getCity();
        this.starName = hotel.getStarName();
        this.business = hotel.getBusiness();
        this.location = hotel.getLatitude() + ", " + hotel.getLongitude();
        this.pic = hotel.getPic();
    }
}

1.2.语法说明

新增文档的DSL语句如下:

POST /{
    
    索引库名}/_doc/1
{
    
    
    "name": "Jack",
    "age": 21
}

对应的java代码:

    @Test
    void testIndexDocument() throws IOException {
    
    
        //1. 创建request对象  索引库名:indexName 文档id:1
        IndexRequest indexRequest = new IndexRequest("indexName").id("1");
        //2. 准备json文档  {"name": "Jack","age": 21}
        indexRequest.source("{\"name\": \"Jack\",\"age\": 21}", XContentType.JSON);
        //3. 发送请求
        client.index(indexRequest, RequestOptions.DEFAULT);
    }

可以看到与创建索引库类似,同样是三步走:

  • 1)创建Request对象
  • 2)准备请求参数,也就是DSL中的JSON文档
  • 3)发送请求

变化的地方在于,这里直接使用client.xxx()API,不再需要client.indices()了。

1.3.完整代码

我们导入酒店数据,基本流程一致,但是需要考虑几点变化:

  • 酒店数据来自于数据库,我们需要先查询出来,得到hotel对象
  • hotel对象需要转为HotelDoc对象
  • HotelDoc需要序列化为json格式

因此,代码整体步骤如下:

  • 1)根据id查询酒店数据Hotel
  • 2)将Hotel封装为HotelDoc
  • 3)将HotelDoc序列化为JSON
  • 4)创建IndexRequest,指定索引库名和id
  • 5)准备请求参数,也就是JSON文档
  • 6)发送请求

hotel-demoHotelDocumentTest测试类中,编写单元测试:

    @Test
    void testAddDocument() throws IOException{
    
    
        // 1.根据id查询酒店数据
        Hotel hotel = hotelService.getById(61083L);
        // 2.转换为文档类型
        HotelDoc hotelDoc = new HotelDoc(hotel);
        // 3.将HotelDoc转json
        String json = JSON.toJSONString(hotelDoc);

        // 1.准备Request对象
        IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
        // 2.准备Json文档
        request.source(json, XContentType.JSON);
        // 3.发送请求
        client.index(request, RequestOptions.DEFAULT);
    }

2.查询文档

2.1.语法说明

查询的DSL语句如下:

GET /hotel/_doc/{
    
    id}

非常简单,因此代码大概分两步:

  • 准备Request对象
  • 发送请求

不过查询的目的是得到结果,解析为HotelDoc,因此难点是结果的解析。完整代码如下:

在这里插入图片描述

可以看到,结果是一个JSON,其中文档放在一个_source属性中,因此解析就是拿到_source,反序列化为Java对象即可。

与之前类似,也是三步走:

  • 1)准备Request对象。这次是查询,所以是GetRequest
  • 2)发送请求,得到结果。因为是查询,这里调用client.get()方法
  • 3)解析结果,就是对JSON做反序列化

2.2.完整代码

hotel-demoHotelDocumentTest测试类中,编写单元测试:

    @Test
    void testGetDocumentById() throws IOException{
    
    
        // 1.准备Request
        GetRequest request = new GetRequest("indexName", "1");
        // 2.发送请求,得到响应
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        // 3.解析响应结果
        String json = response.getSourceAsString();

        HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
        System.out.println("hotelDoc = " + hotelDoc);
    }

3.删除文档

删除的DSL为是这样的:

DELETE /hotel/_doc/{
    
    id}

与查询相比,仅仅是请求方式从DELETE变成GET,可以想象Java代码应该依然是三步走:

  • 1)准备Request对象,因为是删除,这次是DeleteRequest对象。要指定索引库名和id
  • 2)准备参数,无参
  • 3)发送请求。因为是删除,所以是client.delete()方法

hotel-demoHotelDocumentTest测试类中,编写单元测试:

    @Test
    void testDeleteDocument() throws IOException {
    
    
        // 1.准备Request
        DeleteRequest request = new DeleteRequest("indexName", "1");
        // 2.发送请求
        client.delete(request, RequestOptions.DEFAULT);
    }

4.修改文档

4.1.语法说明

修改我们介绍两种方式:

  • 全量修改:本质是先根据id删除,再新增
  • 增量修改:修改文档中的指定字段值

RestClientAPI中,全量修改与新增的API完全一致,判断依据是ID

  • 如果新增时,ID已经存在,则修改
  • 如果新增时,ID不存在,则新增

这里不再赘述,我们主要关注增量修改。

代码示例如图:

在这里插入图片描述

与之前类似,也是三步走:

  • 1)准备Request对象。这次是修改,所以是UpdateRequest
  • 2)准备参数。也就是JSON文档,里面包含要修改的字段
  • 3)更新文档。这里调用client.update()方法

4.2.完整代码

hotel-demoHotelDocumentTest测试类中,编写单元测试:

    @Test
    void testUpdateDocument() throws IOException {
    
    
        // 1.准备Request
        UpdateRequest request = new UpdateRequest("hotel", "1");
        // 2.准备请求参数
        request.doc(
                "age", "18",
                "name", "Rose"
        );
        // 3.发送请求
        client.update(request, RequestOptions.DEFAULT);
    }

5.批量导入文档

案例需求:利用BulkRequest批量将数据库数据导入到索引库中。

步骤如下:

  • 利用mybatis-plus查询酒店数据

  • 将查询到的酒店数据(Hotel)转换为文档类型数据(HotelDoc

  • 利用JavaRestClient中的BulkRequest批处理,实现批量新增文档

5.1.语法说明

批量处理BulkRequest,其本质就是将多个普通的CRUD请求组合在一起发送。

其中提供了一个add方法,用来添加其他请求:

在这里插入图片描述

可以看到,能添加的请求包括:

  • IndexRequest,也就是新增
  • UpdateRequest,也就是修改
  • DeleteRequest,也就是删除

因此Bulk中添加了多个IndexRequest,就是批量新增功能了。示例:

在这里插入图片描述

其实还是三步走:

  • 1)创建Request对象。这里是BulkRequest
  • 2)准备参数。批处理的参数,就是其它Request对象,这里就是多个IndexRequest
  • 3)发起请求。这里是批处理,调用的方法为client.bulk()方法

我们在导入酒店数据时,将上述代码改造成for循环处理即可。

5.2.完整代码

hotel-demoHotelDocumentTest测试类中,编写单元测试:

    @Test
    void testBulkRequest() throws IOException {
    
    
        // 批量查询酒店数据
        List<Hotel> hotels = hotelService.list();

        // 1.创建Request
        BulkRequest request = new BulkRequest();
        // 2.准备参数,添加多个新增的request
        for (Hotel hotel : hotels) {
    
    
            // 2.1.转换为文档类型HotelDoc
            HotelDoc hotelDoc = new HotelDoc(hotel);
            // 2.2.创建新增文档的Request对象
            request.add(new IndexRequest("hotel")
                    .id(hotelDoc.getId().toString())
                    .source(JSON.toJSONString(hotelDoc), XContentType.JSON));
        }
        // 3.发送请求
        client.bulk(request, RequestOptions.DEFAULT);
    }

6.小结

文档操作的基本步骤:

  • 初始化RestHighLevelClient
  • 创建XxxRequestXXXIndexGetUpdateDeleteBulk
  • 准备参数(IndexUpdateBulk时需要)
  • 发送请求。调用RestHighLevelClient#.xxx()方法,xxxindexgetupdatedeletebulk
  • 解析结果(Get时需要)

猜你喜欢

转载自blog.csdn.net/qq_37726813/article/details/129999760