image.png

image.png
image.png
因为数据库里面的字段跟我们的索引库的字段其实是有差异的
所以我们必须要整一个跟索引库字段的实体类
是为了实现数据库的数据到索引库的数据的类型转换

  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 TestAddDoument() throws IOException {
  3. //根据ID查询数据
  4. Hotel hotel = hotelService.getById(60363L);
  5. //转换为文档类型
  6. HotelDoc hotelDoc = new HotelDoc(hotel);
  7. //准备requet文档对象
  8. IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
  9. //准备json文档 将文档内容序列化json数据
  10. request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
  11. //发送请求
  12. client.index(request,RequestOptions.DEFAULT);
  13. }

根据ID 查询酒店数据:

image.png
image.png

  1. /**
  2. * 根据ID 查询文档
  3. */
  4. @Test
  5. void TestSelectDoucment() throws IOException {
  6. //获取请求对象 索引库名称和文档的ID
  7. GetRequest getRequest = new GetRequest("hotel","60363");
  8. //发送请求 得到响应
  9. GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);
  10. //解析结果
  11. String json = response.getSourceAsString();
  12. System.out.println(json);
  13. //反序列化
  14. HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
  15. System.out.println(hotelDoc);
  16. }

更新文档:

image.png
全量更新

局部更新:

  1. /**
  2. * 更新
  3. */
  4. @Test
  5. void testUpdateDocument() throws IOException {
  6. //准备request
  7. UpdateRequest request = new UpdateRequest("hotel","60363");
  8. //准备请求参数
  9. request.doc(
  10. //key value
  11. "price","8989789",
  12. "address","北戴河之光"
  13. );
  14. //发送请求
  15. client.update(request,RequestOptions.DEFAULT);
  16. }

删除文档:

  1. /**
  2. * 删除
  3. */
  4. @Test
  5. void testDelet() throws IOException {
  6. //准备request
  7. DeleteRequest request = new DeleteRequest("hotel","60363");
  8. //发送请求
  9. client.delete(request,RequestOptions.DEFAULT);
  10. }

image.png

批量导入文档:

image.png

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