elasticsearch(RestHighLevelClient API操作)(黑马)

操作全是换汤不换药,创建一个request,然后使用client发送就可以了

一、增加索引库数据

java 复制代码
    @Test
    void testAddDocument() throws IOException {
        //从数据库查出数据
        Writer writer = writerService.getById(199);
        //将查出来的数据处理成json字符串
        String json = JSON.toJSONString(writer);

        // 1.准备Request
        IndexRequest request = new IndexRequest("writer").id(String.valueOf(writer.getId()));
        // 2.准备请求参数DSL,其实就是文档的JSON字符串
        request.source(json, XContentType.JSON);
        // 3.发送请求
        client.index(request, RequestOptions.DEFAULT);
    }

二、查询索引库顺序

java 复制代码
    @Test
    void testGetDocumentById() throws IOException {
        // 1.准备Request      // GET /hotel/_doc/{id}
        GetRequest request = new GetRequest("writer", "199");
        // 2.发送请求
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        // 3.解析响应结果
        String json = response.getSourceAsString();

        Writer writer = JSON.parseObject(json, Writer.class);
        System.out.println("writerDoc = " + writer);
    }

三、删除索引库数据

java 复制代码
@Test
    void testDeleteDocumentById() throws IOException {
        // 1.准备Request      // DELETE /hotel/_doc/{id}
        DeleteRequest request = new DeleteRequest("hotel", "61083");
        // 2.发送请求
        client.delete(request, RequestOptions.DEFAULT);
    }

四、修改索引库数据

java 复制代码
@Test
    void testUpdateById() throws IOException {
        // 1.准备Request
        UpdateRequest request = new UpdateRequest("hotel", "61083");
        // 2.准备参数
        request.doc(
                "price", "870"
        );
        // 3.发送请求
        client.update(request, RequestOptions.DEFAULT);
    }

五、批量导入数据

java 复制代码
@Test
    void testBulkRequest() throws IOException {
        List<Writer> list = writerService.list();
        BulkRequest request = new BulkRequest();
        for (Writer writer : list) {
            HotelDoc hotelDoc = new HotelDoc(hotel);
            String json = JSON.toJSONString(writer);
            request.add(new IndexRequest("writer").id(String.valueOf(writer.getId())).source(json, XContentType.JSON));
        }

        client.bulk(request, RequestOptions.DEFAULT);
    }
相关推荐
运维行者_1 小时前
Applications Manager中的Redis监控
大数据·服务器·数据库·人工智能·网络协议
Agent手记6 小时前
跨境电商如何用AI Agent自动运营多平台店铺?企业级「龙虾」矩阵智能体全流程落地指南
大数据·人工智能·ai·矩阵
老陈头聊SEO6 小时前
生成引擎优化(GEO)提升用户体验与内容创作质量的新策略
其他·搜索引擎·seo优化
天青色等烟雨..6 小时前
R+VIC模型融合实践技术应用及未来气候变化模型预测
大数据·人工智能·arcgis·语言模型·数据分析
云栖梦泽在6 小时前
AI安全实战:AI系统应急响应的实战演练案例
大数据·人工智能·安全
暴躁小师兄数据学院7 小时前
【AI大数据工程师特训笔记】第04讲:PostgreSQL 数据库内置函数详解
大数据·数据库·笔记·ai·语言模型
无忧智库7 小时前
电力行业集团数字化转型信息化战略规划方案(PPT)
大数据·人工智能
无忧智库8 小时前
基于5G-A(通感一体)技术的城市低空飞行器实时航线监控底座建设方案(WORD)
大数据·人工智能·5g
ShGamu9 小时前
2026上半年链条输送机厂家全流程服务与选型参考
大数据·人工智能·链条输送机
财经科技10 小时前
伟淼科技发布2026年6月营销热点:消费、文化与成长的全域增长公式
大数据