1.新增文档
我们要将数据库的酒店数据查询出来,写入elasticsearch中。
1.1.索引库实体类
数据库查询后的结果是一个Hotel类型的对象。结构如下:
@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(); } }
当字段类型不一致需要转化时,可以从构造方法上入手。
1.2.语法说明
新增文档的DSL语句如下:
POST /{索引库名}/_doc/1 { "name": "Jack", "age": 21 }
标签:RestClient,hotel,private,name,文档,Integer,操作,String From: https://www.cnblogs.com/kisshappyboy/p/16961838.html