二十四、RestClient操作文档

目录

一、新增文档

1、编写测试代码

二、查询文档

1、编写测试代码

三、删除文档

1、编写测试代码

四、修改文档

1、编写测试代码

五、批量导入文档

批量查询


一、新增文档

1、编写测试代码

java 复制代码
@SpringBootTest
public class HotelDocumentTest {
    private RestHighLevelClient client;
    @Autowired
    private IHotelService iHotelService;

    @Test
    void testInit(){
        System.out.println(client);
    }

    @BeforeEach
    void setUp(){
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.248.152:9200")
        ));
    }

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

    @Test
    void testAddDocument() throws IOException {
        Hotel hotel = iHotelService.getById(61083L);
        HotelDoc hotelDoc = new HotelDoc(hotel);
//      准备request对象
        IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
//      准备Json文档
        request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
//      发送请求
        client.index(request, RequestOptions.DEFAULT);

    }

}

二、查询文档

1、编写测试代码

java 复制代码
    @Test
    void testGetDocument() throws IOException {
//      准备request对象
        GetRequest request = new GetRequest("hotel","61083");
//      接收response
        GetResponse response = client.get(request,RequestOptions.DEFAULT);
//      解析
        String json = response.getSourceAsString();

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

三、删除文档

1、编写测试代码

java 复制代码
    @Test
    void testDeleteDocument() throws IOException {
//      准备request对象
        DeleteRequest request = new DeleteRequest("hotel","61083");
//      发送请求
        client.delete(request,RequestOptions.DEFAULT);
    }

四、修改文档

1、编写测试代码

java 复制代码
    @Test
    void testUpdateDocument() throws IOException {
//      准备request对象
        UpdateRequest request = new UpdateRequest("hotel","61083");
//       准备请求参数
        request.doc(
                "price","952",
                "starName","四钻 "
        );
//      发送请求
        client.update(request,RequestOptions.DEFAULT);
    }

五、批量导入文档

java 复制代码
    void testBulkRequest() throws IOException{
//        批量查询酒店数据
        List<Hotel> hotelList = iHotelService.list();
//        创建Request
        BulkRequest request = new BulkRequest();
//        准备参数
        for (Hotel hotel : hotelList){
//        转换为文档类型HotelDoc
            HotelDoc hotelDoc = new HotelDoc(hotel);
//        创建新增文档的Request对象
            request.add(new IndexRequest("hotel")
                    .id(hotelDoc.getId().toString())
                    .source(JSON.toJSONString(hotelDoc),XContentType.JSON));
        }
//        发送请求
        client.bulk(request,RequestOptions.DEFAULT);
    }
批量查询
相关推荐
CoderYanger9 分钟前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第八弹):MyBatis Generator
java·数据库·spring boot·spring·java-ee·tomcat·mybatis
whitelbwwww1 小时前
CUDA C++实战--中级
开发语言·前端·c++
FungLeo2 小时前
FengCMS · PHP+MySQL 升级实录 · 内容写入一致性:锁等待超时与跨模型子树删除竞态
开发语言·mysql·php·工程实践·fengcms
jaysee-sjc3 小时前
【黑马点评】Day02:优惠券秒杀与分布式锁
java·分布式·nginx·spring·node.js·tomcat·maven
谙弆悕博士3 小时前
多智能体架构详解:市场拍卖式架构(Market-based / 竞价协商)
开发语言·架构·去中心化·多智能体·智能体·拍卖架构
ShineWinsu3 小时前
对于Qt:QMessageBox的解析
开发语言·c++·qt·面试·客户端·qmessagebox·消息对话框
承渊政道4 小时前
【Python编程快速上手—让繁琐工作自动化】(列表:把数据排成队,从下标取值到深拷贝)
开发语言·python·pycharm·自动化·列表
秋天的怀念_David4 小时前
算法一千零一夜 · 第三十五夜 · 恢复风暴
java·程序人生·算法
考虑考虑4 小时前
JDK27中的加密对象的 PEM 编码
java·后端·java ee
行者-全栈开发4 小时前
SpringBoot + GraalVM 原生编译实战:启动时间从8秒到0.5秒的极致优化
java·spring boot·后端