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);
    }
相关推荐
珠海西格电力2 小时前
零碳园区的能源供给成本主要包括哪些方面?
大数据·分布式·微服务·架构·能源
经济元宇宙3 小时前
摄影培训行业百科:机构选择与学习路径全解析
大数据·人工智能·学习
GJGCY3 小时前
企业AI Agent落地架构深度解析:LLM+RAG+RPA+工具调用全流程
大数据·人工智能·ai·数字化·智能体
互联网科技看点4 小时前
数智化人事管理软件系统行业分析与推荐
大数据
2601_957780845 小时前
GPT API工程化接入:从演示验证到生产部署的完整实践
大数据·人工智能·gpt·架构
WL_Aurora5 小时前
YARN资源调度器深度解析 | 架构原理、作业提交流程
大数据·hadoop·yarn
团象科技5 小时前
2026出海技术观察:云API接口迭代的能力边界与业务增量空间
大数据·人工智能
元拓数智6 小时前
AI 自动化工作流,正在重塑企业数据工程的效率边界
大数据·人工智能·ai·自动化·工作流·数据工程
xwz小王子6 小时前
机器人学习十年进化史——从强化学习到VLA的范式变迁
大数据·学习·机器人
老詹图解IT6 小时前
银河麒麟V10(Kylin V10 SP1/SP3)常见报错对照表—2026年5月版
大数据·kylin