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);
    }
相关推荐
TDengine (老段)37 分钟前
TDengine STMT2 API 使用指南
java·大数据·物联网·时序数据库·iot·tdengine·涛思数据
用户Taobaoapi20142 小时前
母婴用品社媒种草效果量化:淘宝详情API+私域转化追踪案例
大数据·数据挖掘·数据分析
G皮T3 小时前
【Elasticsearch】检索排序 & 分页
大数据·elasticsearch·搜索引擎·排序·分页·检索·深度分页
飞询6 小时前
Docker 安装 Elasticsearch 9
elasticsearch·docker
小新学习屋6 小时前
Spark从入门到熟悉(篇三)
大数据·分布式·spark
rui锐rui7 小时前
大数据学习2:HIve
大数据·hive·学习
G皮T7 小时前
【Elasticsearch】检索高亮
大数据·elasticsearch·搜索引擎·全文检索·kibana·检索·高亮
zskj_zhyl11 小时前
智慧养老丨从依赖式养老到自主式养老:如何重构晚年生活新范式
大数据·人工智能·物联网
哲科软件12 小时前
从“电话催维修“到“手机看进度“——售后服务系统开发如何重构客户体验
大数据·智能手机·重构
zzywxc78712 小时前
AI 正在深度重构软件开发的底层逻辑和全生命周期,从技术演进、流程重构和未来趋势三个维度进行系统性分析
java·大数据·开发语言·人工智能·spring