新增文档

为了与索引库操作分离,我们再次参加一个测试类,做两件事情:

  • 初始化RestHighLevelClient
  • 酒店数据在数据库,我们要将数据库的酒店数据查询出来,写入elasticsearch中

    索引库实体类

    数据库查询后的结果是一个Hotel类型的对象。结构如下:

    1. @Data
    2. @TableName("tb_hotel")
    3. public class Hotel {
    4. @TableId(type = IdType.INPUT)
    5. private Long id;
    6. private String name;
    7. private String address;
    8. private Integer price;
    9. private Integer score;
    10. private String brand;
    11. private String city;
    12. private String starName;
    13. private String business;
    14. private String longitude;
    15. private String latitude;
    16. private String pic;
    17. }

    与我们的索引库结构存在差异:

  • longitude和latitude需要合并为location

因此,我们需要定义一个新的类型,与索引库结构吻合:

  1. package cn.itcast.hotel.pojo;
  2. import lombok.Data;
  3. import lombok.NoArgsConstructor;
  4. @Data
  5. @NoArgsConstructor
  6. public class HotelDoc {
  7. private Long id;
  8. private String name;
  9. private String address;
  10. private Integer price;
  11. private Integer score;
  12. private String brand;
  13. private String city;
  14. private String starName;
  15. private String business;
  16. private String location;
  17. private String pic;
  18. public HotelDoc(Hotel hotel) {
  19. this.id = hotel.getId();
  20. this.name = hotel.getName();
  21. this.address = hotel.getAddress();
  22. this.price = hotel.getPrice();
  23. this.score = hotel.getScore();
  24. this.brand = hotel.getBrand();
  25. this.city = hotel.getCity();
  26. this.starName = hotel.getStarName();
  27. this.business = hotel.getBusiness();
  28. this.location = hotel.getLatitude() + ", " + hotel.getLongitude();
  29. this.pic = hotel.getPic();
  30. }
  31. }

新增文档代码

  1. @Test
  2. void testAddDocument() throws IOException {
  3. // 1.根据id查询酒店数据
  4. Hotel hotel = hotelService.getById(61083L);
  5. // 2.转换为文档类型
  6. HotelDoc hotelDoc = new HotelDoc(hotel);
  7. // 3.将HotelDoc转json
  8. String json = JSON.toJSONString(hotelDoc);
  9. // 1.准备Request对象
  10. IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
  11. // 2.准备Json文档
  12. request.source(json, XContentType.JSON);
  13. // 3.发送请求
  14. client.index(request, RequestOptions.DEFAULT);
  15. }

结果

去es查询一下
image.png

查询文档

  1. @Test
  2. void testGetDocumentById() throws IOException {
  3. // 1.准备Request,查询的索引库和文档id
  4. GetRequest request = new GetRequest("hotel", "61083");
  5. // 2.发送请求,得到响应
  6. GetResponse response = client.get(request, RequestOptions.DEFAULT);
  7. // 3.解析响应结果,看上面的结果图,因为文档在响应结果的_source字段里面,所以要得到这个字段
  8. String json = response.getSourceAsString();
  9. //4.Json解析
  10. HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
  11. System.out.println(hotelDoc);
  12. }

删除文档

  1. @Test
  2. void testDeleteDocument() throws IOException {
  3. // 1.准备Request,删除的索引库和文档id
  4. DeleteRequest request = new DeleteRequest("hotel", "61083");
  5. // 2.发送请求
  6. client.delete(request, RequestOptions.DEFAULT);
  7. }

修改文档

在RestClient的API中,全量修改与新增的API完全一致,判断依据是ID:

  • 如果新增时,ID已经存在,则修改
  • 如果新增时,ID不存在,则新增

这里不再赘述,我们主要关注增量修改。

  1. @Test
  2. void testUpdateDocument() throws IOException {
  3. // 1.准备Request,增量修改的索引库和文档id
  4. UpdateRequest request = new UpdateRequest("hotel", "61083");
  5. // 2.准备请求参数,用都好隔开的一对一对的参数,两个一组,字段:值
  6. request.doc(
  7. "price", "952",
  8. "starName", "四钻"
  9. );
  10. // 3.发送请求
  11. client.update(request, RequestOptions.DEFAULT);
  12. }

批量操作

批量处理BulkRequest,其本质就是将多个普通的CRUD请求组合在一起发送。
其中提供了一个add方法,用来添加其他请求:
能添加的请求包括:

  • IndexRequest,也就是新增
  • UpdateRequest,也就是修改
  • DeleteRequest,也就是删除

因此Bulk中添加了多个IndexRequest,就是批量新增功能了,如果添加多个DeleteRequest请求,那就是批量删除。
我们用批量添加来做例子:

  1. @Test
  2. void testBulkRequest() throws IOException {
  3. // 批量查询酒店数据
  4. List<Hotel> hotels = hotelService.list();
  5. // 1.创建Request
  6. BulkRequest request = new BulkRequest();
  7. // 2.准备参数,添加多个新增的Request
  8. for (Hotel hotel : hotels) {
  9. // 2.1.转换为文档类型HotelDoc
  10. HotelDoc hotelDoc = new HotelDoc(hotel);
  11. // 2.2.创建新增文档的Request对象
  12. request.add(new IndexRequest("hotel")
  13. .id(hotelDoc.getId().toString())
  14. .source(JSON.toJSONString(hotelDoc), XContentType.JSON));
  15. }
  16. // 3.发送请求
  17. client.bulk(request, RequestOptions.DEFAULT);
  18. }

:::success BulkRequest批量操作和循环发请求执行增删该操作的区别在于,BulkRequest只用发一次请求,而循环要发很多次请求 :::