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);
    }
相关推荐
阿豪344 分钟前
2025 年职场转行突围:除实习外,这些硬核证书让你的简历脱颖而出(纯经验分享)
大数据·人工智能·经验分享·科技·信息可视化·产品经理
张驰课堂1 小时前
老树发新芽:六西格玛培训为石油机械制造注入持久活力
大数据·人工智能·制造
卡卡_R-Python1 小时前
大数据探索性分析——抽样技术应用
大数据·r
伍哥的传说2 小时前
Lodash-es 完整开发指南:ES模块化JavaScript工具库实战教程
大数据·javascript·elasticsearch·lodash-es·javascript工具库·es模块·按需导入
请提交用户昵称2 小时前
大数据各组件flume,datax,presto,DolphinScheduler,findBI在大数据数仓架构中的作用和功能。
大数据·flume·datax·dolphin·presto·findbi·大数据组件
IT果果日记2 小时前
详解DataX开发达梦数据库插件
大数据·数据库·后端
用户Taobaoapi20143 小时前
微店API秘籍!轻松获取商品详情数据
大数据·数据挖掘·数据分析
chimchim664 小时前
StarRocks导入数据-使用 Broker Load 进行异步导入
大数据·sql
iGarment5 小时前
服装采购跟单系统的高效管理实践
大数据·经验分享·云计算
闯闯桑5 小时前
Spark 中spark.implicits._ 中的 toDF和DataFrame 类本身的 toDF 方法
大数据·ajax·spark·scala