Springboot集成solr实现将数据库表数据导入索引库、检索数据、数据删除等
一、创建Springboot项目、准备数据库表。


二、在solrhome里做如下配置:
旧版本solr在schema.xml里配置,新版本的solr在managed-schema文件配置
新版本在创建Core时虽然指定了schema.xml但创建成功后你会发现并没有没这个文件。
text_ik为我自己添加配置的中文分词器IKAnalyzer。
<!--基本域 对应book表--> <field name="book_id" type="pint" indexed="true" stored="true"/> <field name="book_title" type="text_ik" indexed="true" stored="true"/> <field name="book_author" type="text_ik" indexed="true" stored="true"/> <field name="book_price" type="pdouble" indexed="true" stored="true"/> <field name="book_describe" type="text_ik" indexed="true" stored="true"/> <field name="book_count" type="text_ik" indexed="true" stored="true"/> <field name="book_time" type="pdate" indexed="true" stored="true"/> <!--将某一个域中的数据复制到另一个域中--> <field name="book_keywords" type="text_ik" indexed="true" stored="true" multiValued="true"/> <copyField source="book_title" dest="book_keywords"/> <copyField source="book_author" dest="book_keywords"/> <copyField source="book_describe" dest="book_keywords"/> <!--动态域 special下有尺寸和版本 其值是不确定的--> <dynamicField name="book_special_*" type="string" indexed="true" stored="true"/>
三、导入solr依赖。
当然还有Spring基本jar包,数据库相关jar,mybatis....等基本jar包自行导入即可。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-solr</artifactId> </dependency>
四、application.yml配置solr地址
server: port: 8088 spring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/test?useUnocode=true&characterEncoding=utf-8&serverTimezone=UTC type: com.alibaba.druid.pool.DruidDataSource data: solr: host: http://localhost:8087/solr mybatis: type-aliases-package: com.star.pojo mapper-locations: classpath:mapper/*.xml
五、在实体类中字段添加注解@Field,名字与schema.xml/managed-schema文件配置的名字保持一致,id上不要添加。
@Data public class Book implements Serializable { //@Field("book_id") private Integer id; @Field("book_author") private String author; @Field("book_title") private String title; @Field("book_price") private Double price; @Field("book_describe") private String describe; @Field("book_count") private Integer count; @Field("book_time") private Date time; private String special; @Dynamic //动态域--必须是map @Field("book_special_*") private Map<String,String> specMap; private static final long serialVersionUID = 1L; }
六、向索引库手动写入死数据
Controller:
1 @Autowired 2 private SolrTemplate solrTemplate; 3 @RequestMapping("/setData") 4 public String setData(){ 5 Book book = new Book(); 6 book.setId(22); 7 book.setAuthor("xi"); 8 book.setTitle("asa"); 9 book.setDescribe("sssss"); 10 book.setCount(40); 11 book.setPrice(23.5); 12 13 Map<String,String> map = new HashMap<>(); 14 map.put("size", "大"); 15 map.put("version", "1.2"); 16 book.setSpecMap(map); 17 18 book.setTime(new Date()); 19 20 this.solrTemplate.saveBean("connection1",book); 21 this.solrTemplate.commit("connection1"); 22 return "写入成功"; 23 }
在solr网页客户端中可以看到写入成功了:

七、将数据库表中的数据写入索引库
@Autowired private BookService bookService; @Autowired private SolrTemplate solrTemplate; @RequestMapping("/setData2") public String setData2(){ //查找数据库book表中的数据 List<Book> list = this.bookService.getAll(); this.solrTemplate.saveBeans("connection1",list); this.solrTemplate.commit("connection1"); return "写入成功"; }
在solr网页客户端中可以看到写入的数据:

八、搜索关键词模拟全局搜索。
这里以复制域的关键词为例
1 @RequestMapping("/getData") 2 public List<Book> getData(String a){ 3 4 Query query = new SimpleQuery("*:*"); 5 6 Criteria criteria = new Criteria("book_keywords").is(a); 7 query.addCriteria(criteria); 8 query.setRows(10); 9 10 Page<Book> books = this.solrTemplate.query("connection1", query, Book.class); 11 List<Book> list = books.getContent(); 12 return list; 13 }
可以看到搜索到了包含”非常“的数据了:

九、删除索引入库的数据,可以直接传List进来,这里删除单个。
1 @RequestMapping("/delData") 2 public String delData(int a){ 3 SimpleQuery query = new SimpleQuery(); 4 Criteria criteria = new Criteria("id").is(a); 5 query.addCriteria(criteria); 6 this.solrTemplate.delete("connection1",query); 7 this.solrTemplate.commit("connection1"); 8 return "删除ok了"; 9 }

在索引库查找发现删除成功了。


浙公网安备 33010602011771号