JavaRestClient实现文档的CRUD

  1. 初始化JavaRestClient
    略,与上一篇一致

  2. 创建代码
    通过注入service查询出数据库中数据
    将查询出来的bean转换成es能保存的bean(如坐标等)
    创建indexRequest,通过指定索引,指定ID
    准备Json文档
    发送请求

package com.yy.hotel;

import com.alibaba.fastjson.JSON;
import com.yy.hotel.pojo.Hotel;
import com.yy.hotel.pojo.HotelDoc;
import com.yy.hotel.service.IHotelService;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

import static com.yy.hotel.constants.HotelConstants.MAPPING_TEMPLATE;

@SpringBootTest
public class HotelDocumentTest {
    @Autowired
    private IHotelService hotelService;


    private RestHighLevelClient restHighLevelClient;

    @Test
    void testInit(){
        System.out.println(restHighLevelClient);
    }



    @Test
    void testAddDocument() throws IOException {
//        id根据注入的service查询数据库获取
        Hotel hotel = hotelService.getById(36934L);
//        转换为文档类型,由于索引库与数据库的表有细微差别,所以多加了一个HotelDoc用于转换
        HotelDoc hotelDoc = new HotelDoc(hotel);

//        1.创建Request对象,索引库中对ID要求都是字符串,所以要toString
        IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
//        2.准备json部分,将对象转换成json
        request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
        restHighLevelClient.index(request, RequestOptions.DEFAULT);

    }


//    每个单元测试中都会用,所以定义成成员变量
//    同时提交完成变更初始化就使用@BeforeEach
    @BeforeEach
    void setUp(){
        this.restHighLevelClient = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.0.106:9200")
        ));
    }



//    测试完成销毁
    @AfterEach
    void tearDown() throws IOException {
        this.restHighLevelClient.close();
    }
}

可在kibana中进行验证

GET /hotel/_doc/36934

查询文档

package com.yy.hotel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yy.hotel.pojo.Hotel;
import com.yy.hotel.pojo.HotelDoc;
import com.yy.hotel.service.IHotelService;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

import static com.yy.hotel.constants.HotelConstants.MAPPING_TEMPLATE;

@SpringBootTest
public class HotelDocumentTest {
    @Autowired
    private IHotelService hotelService;


    private RestHighLevelClient restHighLevelClient;

    @Test
    void testInit(){
        System.out.println(restHighLevelClient);
    }





    @Test
    void testGetDocument() throws IOException {

//        1.创建Request对象,指定索引与ID
        GetRequest request = new GetRequest("hotel","36934");
//        2.发送请求
        GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
//        3.解析响应结果
        String json = response.getSourceAsString();

        HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
        System.out.println(hotelDoc);

    }


//    每个单元测试中都会用,所以定义成成员变量
//    同时提交完成变更初始化就使用@BeforeEach
    @BeforeEach
    void setUp(){
        this.restHighLevelClient = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.0.106:9200")
        ));
    }




//    测试完成销毁
    @AfterEach
    void tearDown() throws IOException {
        this.restHighLevelClient.close();
    }
}


在这里插入图片描述
更新文档
同样也有局部更新和全量更新
只演示局部更新

package com.yy.hotel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yy.hotel.pojo.Hotel;
import com.yy.hotel.pojo.HotelDoc;
import com.yy.hotel.service.IHotelService;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

import static com.yy.hotel.constants.HotelConstants.MAPPING_TEMPLATE;

@SpringBootTest
public class HotelDocumentTest {
    @Autowired
    private IHotelService hotelService;


    private RestHighLevelClient restHighLevelClient;

    @Test
    void testInit(){
        System.out.println(restHighLevelClient);
    }

    @Test
    void testUpdateDocument() throws IOException {

//        1.创建Request对象,指定索引与id
        UpdateRequest request = new UpdateRequest("hotel", "36934");

//        2.准备请求参数
        request.doc(
                "price","250"
        );
//        3.发送请求
        restHighLevelClient.update(request, RequestOptions.DEFAULT);


    }


//    每个单元测试中都会用,所以定义成成员变量
//    同时提交完成变更初始化就使用@BeforeEach
    @BeforeEach
    void setUp(){
        this.restHighLevelClient = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.0.106:9200")
        ));
    }






//    测试完成销毁
    @AfterEach
    void tearDown() throws IOException {
        this.restHighLevelClient.close();
    }
}

删除文档

@Test
    void testDeleteDocument() throws IOException {

//        1.创建Request对象,指定索引与id
        DeleteRequest request = new DeleteRequest("hotel", "36934");

//        2.发送请求
        restHighLevelClient.delete(request, RequestOptions.DEFAULT);


    }

批量导入数据

  1. 利用mybatis-plus查询多条数据。
  2. 将数据hotel转换为文档类型格式。
  3. 利用javaRestClient的Bluk批处理,实现批量新增文档,示例代码如下:
@Test
    void testBulkDocument() throws IOException {
//        数据库中批量查询数据
        List<Hotel> hotels = hotelService.list();

//        1.创建Request对象
        BulkRequest request = new BulkRequest();
//        2.准备参数,添加多个新增Request
        //        转换为文档类型
        for (Hotel hotel : hotels) {
            HotelDoc hotelDoc = new HotelDoc(hotel);
//            创建新增文档的Request对象
            request.add(new IndexRequest("hotel")
                    .id(hotelDoc.getId().toString())
                    .source(JSON.toJSONString(hotelDoc),XContentType.JSON));
        }
//        3.发送请求
        restHighLevelClient.bulk(request, RequestOptions.DEFAULT);


在kibana中查询索引中所有值,判定是否都批量添加成功

GET /hotel/_search
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值