RestClient基本操作

RestClient客户端

我们在spring项目中使用RestHighLevelClient客户端工具连接ES

客户端连接

pom引入

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.12.1</version>
</dependency>

在application中注入或者写成config配置都可以

@Bean
public RestHighLevelClient RestHighLevelClient(){
    return new RestHighLevelClient(RestClient.builder(
            HttpHost.create("http://ip:9200")));
}

测试是否成功连接

private RestHighLevelClient client;
@Autowired
private HotelService hotelService;
/**
 * @description: 测试配置是否成功
*/
@Test
void test(){
    System.out.println(client);
}
//输出
org.elasticsearch.client.RestHighLevelClient@1c8f6c66

那我们连接成功后开始测试方法

索引操作

创建index

@Test
void createHotelIndex() throws IOException {
    //创建Request
    CreateIndexRequest request = new CreateIndexRequest("hotel");
    //DSL语句
    request.source(MAPPING_TEMPLATE, XContentType.JSON);
    //发送请求
    client.indices().create(request, RequestOptions.DEFAULT);
}

这里的MAPPING_TEMPLATE就是我们创建index的DSL语句

我的参数

在这里插入图片描述

截取部分,大家替换自己写的DSL即可

删除index

@Test
void deleteHotelIndex() throws IOException {
    //创建Request
    DeleteIndexRequest request = new DeleteIndexRequest("hotel");
    //发送请求
    client.indices().delete(request, RequestOptions.DEFAULT);
}

存在index

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

文档操作

文档单个添加

@Test
void testAddDocument() throws IOException {
    //创建Request
    Hotel hotel = hotelService.getById(60916L);
    HotelDoc hotelDoc = new HotelDoc(hotel);
    IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
    request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
    client.index(request,RequestOptions.DEFAULT);
}

文档批量添加

/**
 * @description: 批量加入数据
 * 批量查询、GET/hotel/_search
 * 单条查询、GET/hotel/_doc/id值
*/
@Test
void testBulkRequest() throws IOException {
    List<Hotel> list = hotelService.list();
    //创建request
    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);
}

查询文档

@Test
void testGetDocumentByID() throws IOException {
    //准备request
    GetRequest request = new GetRequest("hotel", "60916");
    //发送请求
    GetResponse response = client.get(request, RequestOptions.DEFAULT);
    //解析响应结果
    String json = response.getSourceAsString();
    HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
    System.out.println(hotelDoc);

}

删除文档

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

修改文档

@Test
void testUpdateDocumentByID() throws IOException {
    //准备request
    UpdateRequest request = new UpdateRequest("hotel","60916");
    //准备请求参数
    request.doc(
            "price","999",
            "startName","五钻"
    );
    //发送请求
    client.update(request,RequestOptions.DEFAULT);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值