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);
    }
相关推荐
得物技术18 小时前
从埋点需求到规则资产:Hermes Agent 重构得物数仓工作流
大数据·llm·ai编程
久美子19 小时前
AI驱动数仓建设的Harness工程实践——本体建模、知识分层与上下文工程
大数据
大树881 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
大志哥1231 天前
ES和Logstash日志链路系统上线后遭遇切片爆炸(解决)
大数据·elasticsearch
果丁智能2 天前
物联网智能锁赋能集中式住宿:身份核验与远程权限管控的全链路技术实践
大数据·人工智能·物联网·智能家居
ApacheSeaTunnel2 天前
实战演示 | 基于 Apache SeaTunnel 与 Apache DolphinScheduler 实现 MySQL 到 Doris 离线定时增量同步
大数据·mysql·开源·doris·数据集成·seatunnel·数据同步
weixin_397574092 天前
PDF复杂表格的1:1还原引擎:跨页表格自动拼接技术实战
大数据·人工智能·pdf
TableRow2 天前
参数化搜索的实现原理:从多维索引到查询优化
elasticsearch·全文检索
极光代码工作室2 天前
基于数据仓库的电商数据分析平台
大数据·hadoop·python·spark·数据可视化
秋名山码民2 天前
Graph RAG 深度解析:从向量检索到知识推理的技术演进
大数据·人工智能·rag