我们需要将数据库的酒店数据查询出来,写入elasticsearch中。
索引库实体类
@Data @TableName("tb_hotel") public class Hotel { @TableId(type = IdType.INPUT) private Long id; private String name; private String address; private Integer price; private Integer score; private String brand; private String city; private String starName; private String business; private String longitude; private String latitude; private String pic; }
-
longitude和latitude需要合并为location
因此,我们需要定义一个新的类型,与索引库结构吻合:
@Data @NoArgsConstructor public class HotelDoc { private Long id; private String name; private String address; private Integer price; private Integer score; private String brand; private String city; private String starName; private String business; private String location; private String pic; public HotelDoc(Hotel hotel) { this.id = hotel.getId(); this.name = hotel.getName(); this.address = hotel.getAddress(); this.price = hotel.getPrice(); this.score = hotel.getScore(); this.brand = hotel.getBrand(); this.city = hotel.getCity(); this.starName = hotel.getStarName(); this.business = hotel.getBusiness(); this.location = hotel.getLatitude() + ", " + hotel.getLongitude(); this.pic = hotel.getPic(); } }
DSL语句:
-
1)创建Request对象
-
2)准备请求参数,也就是DSL中的JSON文档
-
3)发送请求
变化的地方在于,这里直接使用client.xxx()的API,不再需要client.indices()了。
单元测试
@Test void testAddDocument() throws IOException { // 1.根据id查询酒店数据 Hotel hotel = hotelService.getById(61083L); // 2.转换为文档类型 HotelDoc hotelDoc = new HotelDoc(hotel); // 3.将HotelDoc转json String json = JSON.toJSONString(hotelDoc); // 1.准备Request对象 IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString()); // 2.准备Json文档 request.source(json, XContentType.JSON); // 3.发送请求 client.index(request, RequestOptions.DEFAULT); }
GET /hotel/_doc/{id}
可以看到,结果是一个JSON,其中文档放在一个_source
属性中,因此解析就是拿到_source
,反序列化为Java对象即可。
与之前类似,也是三步走:
1)准备Request对象。这次是查询,所以是GetRequest。
2)发送请求,得到结果。因为是查询,这里调用client.get()方法。
3)解析结果,就是对JSON做反序列化。
单元测试
1 /** 2 * 根据文档id查询数据 3 * @throws IOException 4 */ 5 @Test 6 void testGetDocumentById() throws IOException { 7 // 1.准备Request // GET /hotel/_doc/{id} 8 GetRequest request = new GetRequest("hotel", "61083"); 9 // 2.发送请求 10 GetResponse response = client.get(request, RequestOptions.DEFAULT); 11 // 3.解析响应结果 12 String json = response.getSourceAsString(); 13 14 HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class); 15 System.out.println("文档id为61083的数据为:" + hotelDoc); 16 }
运行结果
删除文档
DSL语句:DELETE /hotel/_doc/{id}
与查询相比,仅仅是请求方式从DELETE变成GET,可以想象Java代码应该依然是三步走:
1)准备Request对象,因为是删除,这次是DeleteRequest对象。要指定索引库名和id。
2)准备参数,无参。
3)发送请求。因为是删除,所以是client.delete()方法。
单元测试
1 /** 2 * 根据文档id删除数据 3 * @throws IOException 4 */ 5 @Test 6 void testDeleteDocumentById() throws IOException { 7 // 1.准备Request // DELETE /hotel/_doc/{id} 8 DeleteRequest request = new DeleteRequest("hotel", "61083"); 9 // 2.发送请求 10 client.delete(request, RequestOptions.DEFAULT); 11 }
修改文档
两种方式:
-
全量修改:本质是先根据id删除,再新增。
-
增量修改:修改文档中的指定字段值。
在RestClient的API中,全量修改与新增的API完全一致,判断依据是ID:
-
如果新增时,ID已经存在,则修改。
-
如果新增时,ID不存在,则新增。
与之前类似,也是三步走:
-
1)准备Request对象。这次是修改,所以是UpdateRequest
-
2)准备参数。也就是JSON文档,里面包含要修改的字段
-
3)更新文档。这里调用client.update()方法
单元测试
1 /** 2 * 根据文档id修改数据 3 * @throws IOException 4 */ 5 @Test 6 void testUpdateById() throws IOException { 7 // 1.准备Request 8 UpdateRequest request = new UpdateRequest("hotel", "61083"); 9 // 2.准备参数 10 request.doc( 11 "price", "800" 12 ); 13 // 3.发送请求 14 client.update(request, RequestOptions.DEFAULT); 15 }