elasticsearch(二):RestClient操作文档

一,利用JavaRestClient实现文档的CRUD

目标:去数据库查询酒店数据,导入到hotel索引库,实现酒店数据的CRUD

1.初始化JavaRestClient 

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

    @AfterEach
    void tearDown() throws IOException {
        client.close();
    }

2.添加酒店数据到索引库

 @Test
    void testAddDocument() throws IOException {
        // 1.查询数据库hotel数据
        Hotel hotel = hotelService.getById(61083L);
        // 2.转换为HotelDoc
        HotelDoc hotelDoc = new HotelDoc(hotel);
        // 3.转JSON
        String json = JSON.toJSONString(hotelDoc);

        // 1.准备Request
        IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
        // 2.准备请求参数DSL,其实就是文档的JSON字符串
        request.source(json, XContentType.JSON);
        // 3.发送请求
        client.index(request, RequestOptions.DEFAULT);
    }

3.根据id查询酒店数据

    @Test
    void testGetDocumentById() throws IOException {
        // 1.准备Request      // GET /hotel/_doc/{id}
        GetRequest request = new GetRequest("hotel", "61083");
        // 2.发送请求
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        // 3.解析响应结果
        String json = response.getSourceAsString();

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

4.根据id修改酒店数据

  @Test
    void testUpdateById() throws IOException {
        // 1.准备Request
        UpdateRequest request = new UpdateRequest("hotel", "61083");
        // 2.准备参数
        request.doc(
                "price", "870"
        );
        // 3.发送请求
        client.update(request, RequestOptions.DEFAULT);
    }

5.根据id删除文档数据

    @Test
    void testDeleteDocumentById() throws IOException {
        // 1.准备Request      // DELETE /hotel/_doc/{id}
        DeleteRequest request = new DeleteRequest("hotel", "61083");
        // 2.发送请求
        client.delete(request, RequestOptions.DEFAULT);
    }

文档操作的基本步骤:

1.初始化RestHighLevelClient

2,创建XxxRequestXXXIndexGetUpdateDelete

3.准备参数(IndexUpdate时需要)

4.发送请求。调用RestHighLevelClient#.xxx()方法,xxxindexgetupdatedelete

      5.解析结果(Get时需要)

二,利用JavaRestClient批量导入酒店数据到ES

需求:批量查询酒店数据,然后批量导入索引库中

     1.利用mybatis-plus查询酒店数据

      2.将查询到的酒店数据(Hotel)转换为文档类型数据(HotelDoc

      3.利用JavaRestClient中的Bulk批处理,实现批量新增文档,示例代码如下

@Test
    void testBulkRequest() throws IOException {
        // 查询所有的酒店数据
        List list = hotelService.list();

        // 1.准备Request
        BulkRequest request = new BulkRequest();
        // 2.准备参数
        for (Hotel hotel : list) {
            // 2.1.转为HotelDoc
            HotelDoc hotelDoc = new HotelDoc(hotel);
            // 2.2.转json
            String json = JSON.toJSONString(hotelDoc);
            // 2.3.添加请求
            request.add(new IndexRequest("hotel").id(hotel.getId().toString()).source(json, XContentType.JSON));
        }

        // 3.发送请求
        client.bulk(request, RequestOptions.DEFAULT);
    }

你可能感兴趣的:(elasticsearch,java)