上一篇我们已经写到了对索引库的操作,现在我们要更进一步,对文档document及后面的操作:
- 我们现在添加文档到索引库(相当于MySQL添加一条记录到table当中)
- 我们新建立了一个HotelDocumentTest测试类
@Test//添加文档到索引库 void testIndexDocument() throws IOException { //GET /hotel/_doc/1 IndexRequest request = new IndexRequest("hotel").id("1"); request.source("{\"name\":\"zs\",\"city\":\"长沙\"}",XContentType.JSON); client.index(request,RequestOptions.DEFAULT); //在index这里创建倒排索引 }
- 刚刚我们测试了添加一条记录。但是我们现在需要将MySQL当中的hotel表的所有记录导入hotel索引库,那么我们需要建两个实体类,一个对应MySQL,一个对应es索引库,然后将两个实体类进行关联,从而将MySQL的hotel表和es的索引库进行关联
- 首先我们创建对应MySQL的实体类
@TableName("tb_hotel") public class Hotel { @TableId(type = IdType.AUTO) 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 latitude; private String longitude; private String pic; }
- 然后我们需要用到mybatis-plus来操作MySQL数据库,所以需要导入这两个依赖
<!--整合mybatis-plus--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.49</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency>
- 然后建一个对应hotel索引库的实体类:(构造函数location那里不一样)
- 思路:我们是是将MySQL对应的hotel实体类的对象作为参数,传进索引库的构造方法里面来对索引库对象对应的属性进行初始化
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; /*经纬度换成location*/ private String location; private String pic; public HotelDoc() { } /*构造函数*/ 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(); } }
- 然后写hotelMapper,继承BaseMapper
- 再写hotelService,继承苞米豆的IService
- 然后写他的实现类,我们是继承了mybatis-plus提供的ServiceImpl
- 紧接着我们写service的测试类
- 我们既然要注入es客户端,那么我们容器当中就需要有这个es客户端,所以我们去启动类配置,并且将启动类配置好扫描器:
- 扫描器:@MapperScan("com.pro.mapper")
@Bean public RestHighLevelClient client(){ return new RestHighLevelClient( RestClient.builder(HttpHost.create("http://192.168.8.171:9200")) ); }
- 既然要连接MySQL数据库,那么我们需要去核心配置文件写上我们的配置
- 数据库连接四大金刚
- mybatis.xml文件扫描包的配置
- mapper别名配置
- 开启驼峰命名
#mysql spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://192.168.8.171:3306/hotel spring.datasource.username=root spring.datasource.password=root #扫描包 mybatis-plus.mapper-locations=classpath:mapper/*.xml #别名 mybatis-plus.type-aliases-package=com.pro.domain #驼峰 mybatis-plus.configuration.map-underscore-to-camel-case=true
- 执行测试类之后,我们去查一下是否有这个文档记录,
- source里面就是我们加进来的内容
- get /hotel/_doc/38665
那么我们查询MySQL记录并将其加入索引库成功了!
本文含有隐藏内容,请 开通VIP 后查看