黑马JavaRestClient操作索引库

1、初始化RestClient

        1、引入es的RestHighLevelClient的依赖

<!--RestHighLevelClient-->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

        2、因为SpringBoot默认的ES版本是7.6.2,所以我们需要覆盖默认的ES版本:

        3、初始化

RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
        HttpHost.create("http://192.168.133.129:9200") //此处填写你的es地址和端口
));

2、创建索引库

//创建Request对象
CreateIndexRequest request = new CreateIndexRequest("hotel");
//准备请求的参数,DSL语句
request.source(MAPPING_TEMPLATE, XContentType.JSON);
//发送请求
client.indices().create(request, RequestOptions.DEFAULT);

如有报错,可见隔壁大哥的提示org.elasticsearch.common.compress.NotXContentException: Compressor detection can only be called on s-CSDN博客

@Test
void testHotelIsExist() throws IOException {
    //创建Request对象
    GetIndexRequest request = new GetIndexRequest("hotel");
    //发送请求
    boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
    System.out.println(exists);
}

@Test
void testHotelDelete() throws IOException {
    //创建Request对象
    DeleteIndexRequest request = new DeleteIndexRequest("hotel");
    client.indices().delete(request,RequestOptions.DEFAULT);
}

删除和判空索引库操作大同小异

索引库操作的基本步骤:
        初始化RestHighLevelClient
        创建XxxIndexRequest。XXX是CREATE、Get、Delete
        准备DSL(CREATE时需要)
        发送请求。调用RestHighLevelClient#indices().xxx()方法 xxx是create、exists、delete

3、利用JavaRestClient实现文档的CRUD

去数据库查询酒店数据,导入到hotel索引库,实现酒店数据的CRUD, 基本步骤如下:

1.初始化JavaRestClient

@BeforeEach
void setUp() {
    this.client = new RestHighLevelClient(RestClient.builder(
            HttpHost.create("http://192.168.133.129:9200")
    ));
}

2.利用JavaRestClient新增酒店数据

@Test
void testAddDocument() throws IOException {
    Hotel hotel = iHotelService.getById(36934L);//通过mybatisplus获取酒店数据
    HotelDoc hotelDoc = new HotelDoc(hotel);//转换成文档接收的格式
    //1、准备Request对象
    IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
    //2、准备Json文档
    request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);//转换成JSON并发送
    //3、发送
    client.index(request,RequestOptions.DEFAULT);
}

3.利用JavaRestClient根据id查询酒店数据

@Test
void testGetDocument() throws IOException {
    GetRequest request = new GetRequest("hotel").id("36934");
    GetResponse response = client.get(request, RequestOptions.DEFAULT);
    String hotel = response.getSourceAsString();
    System.out.println(hotel);
}

4.利用JavaRestClient删除酒店数据

@Test
void testDeleteDocument() throws IOException {
    DeleteRequest request = new DeleteRequest("hotel").id("36934");
    client.delete(request,RequestOptions.DEFAULT);
}

5.利用JavaRestClient修改酒店数据

@Test
void testUpdateDocument() throws IOException {
    UpdateRequest request = new UpdateRequest("hotel","36934");
    request.doc(
            "price","666",
            "starName","一百钻"
    );
    client.update(request,RequestOptions.DEFAULT);
}

文档操作的基本步骤:
        初始化RestHighLevelClient
        创建XxxRequest。XXX是Index、Get、Update、Delete
        准备参数(Index和Update时需要)
        发送请求。调用RestHighLevelClient#.xxx()方法,xxx是index、get、update、delete

6.批量插入酒店数据

@Test
void testBulkRequest() throws IOException {
    List<Hotel> list = iHotelService.list();
    BulkRequest request = new BulkRequest();
    for (Hotel hotel : list) {
        HotelDoc hotelDoc = new HotelDoc(hotel);
        request.add(
                new IndexRequest("hotel").id(hotelDoc.getId().toString()).source(JSON.toJSONString(hotelDoc),XContentType.JSON)
        );
    }
    client.bulk(request,RequestOptions.DEFAULT);
}

  • 27
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值