RestHightLevelCline快速入门

RestHightLevelCline模板

代码

@SpringBootTest
public class EsTest {
    //操作ES的对象,类似于RedisTemplate
    private RestHighLevelClient client;

    @Autowired
    private HotelService service;

    //对象创建完成时执行
    @BeforeEach
    void setUp() {
        this.client = new RestHighLevelClient(
                RestClient.builder(
                        HttpHost.create("http://192.168.207.20:9200")));
    }
    //对象使用结束后执行
    @AfterEach
    void tearDown() throws IOException {
        this.client.close();
    }

    //测试resthighlevelclient
    @Test
    void init() {
        System.out.println("111111111111111");
    }

    //测试创建索引
    @Test
    void testCreateHotelIndex() throws IOException {
        CreateIndexRequest request = new CreateIndexRequest("hotel");
        request.source(MAPPING_TEMPLATE, XContentType.JSON);
        client.indices().create(request, RequestOptions.DEFAULT);
    }

    //    测试删除索引
    @Test
    void testDelHotelIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("hotel");
        client.indices().delete(request, RequestOptions.DEFAULT);
    }

    //   测试索引库是否存在
    @Test
    void testExistsHotelIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest("hotel");
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }


    //    添加一条文档
    @Test
    void testAddHotelDoc() throws IOException {
        Hotel hotel = service.getById(38609L);
        HotelDoc hotelDoc = new HotelDoc(hotel);
        String json = JSON.toJSONString(hotelDoc);
        IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
        request.source(json, XContentType.JSON);
        client.index(request, RequestOptions.DEFAULT);
    }

    //获取一条文档
    @Test
    void testGetHotelDoc() throws IOException {
        GetRequest request = new GetRequest("hotel").id("38609");
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        String json = response.getSourceAsString();
        System.out.println(json);
    }

    //    删除一条文档
    @Test
    void testDelHotelDoc() throws IOException {
        DeleteRequest request = new DeleteRequest("hotel", "38609");
        client.delete(request, RequestOptions.DEFAULT);
    }

    //    更新一条语句
    @Test
    void testUpdateHotelDoc() throws IOException {
        UpdateRequest request = new UpdateRequest("hotel", "38609");
        request.doc("address", "广灵二路888号", "business", "哈尔滨市道里区上海路");
        client.update(request, RequestOptions.DEFAULT);
    }
//    测试批量添加
    @Test
    void testBunkAddHotelDoc() throws IOException {
        BulkRequest request = new BulkRequest("hotel");
        List<Hotel> list = service.list();
        for (Hotel hotel : list) {
            HotelDoc hotelDoc = new HotelDoc(hotel);
            request.add(new IndexRequest()
                    .id(hotelDoc.getId().toString())
                    .source(JSON.toJSONString(hotelDoc),XContentType.JSON));
        }

        client.bulk(request,RequestOptions.DEFAULT);
    }
}

依赖

如果你用了springboot 的父工程管理了依赖的版本,那么你在引入ES的依赖是就需要再次声明依赖的版本,因为父工程的默认依赖版本比较低。

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.10.RELEASE</version>
     	<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
        <java.version>1.8</java.version>
        <elasticsearch.version>7.12.1</elasticsearch.version>
</properties>

<dependency>
         <groupId>org.elasticsearch.client</groupId>
         <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值