学习了一下https://github.com/TyCoding/ssm-redis-solr这个github上的solr搜索功能,如今来记录一下。html
个人理解就是solr有点相似于数据库,但它是有索引的数据库,按不少字段创建索引,多是b+树或者散列索引,而后就可以实现海量数据的查找。solr经过导入jar包就能够对这个库就行增删改查了,后端逃不掉的增删改查。。。java
具体我就不说了,由于我是直接用了github上配置好的,毕竟站在巨人的肩膀上学习嘛git
地址:https://github.com/TyCoding/solr-tomcatgithub
访问端口:localhost:8080/solr/index.htmlweb
这里的new_core就是项目中配置的路径,就将商品的索引放在这里。redis
而后用Test测试它的使用,测试的时候要引入配置文件,否则会致使空指针错误,我竟然如今才知道。怪不得之前只要用Autowired的时候就会空指针错误。。,并且还要@Runwith注解,引入包import org.springframework.test.context.junit4.*;eclipse点击不会有import提示,须要本身加上去。spring
这里新建了一个实体对象,而后把这个实体对象加入到索引库里,在solr索引库里面就能够找到这个字段数据库
在new_core的schema里面就以Id建好了索引后端
以及不少的信息tomcat
@Test public void testFindById() { Goods goods = solrTemplate.getById(1, Goods.class); System.out.println("--------" + goods.getTitle()); }
经过id查找,控制台会输出你刚刚插入的数据,也就是经过solrTemplate找到了你的数据。
@Test public void testAddList() { List<Goods> list = new ArrayList<Goods>(); //循环插入100条数据 for (int i = 0; i < 100; i++) { BigDecimal price=new BigDecimal (2.3); Goods goods = new Goods(i + 1L, "华为Mate" + i,price, "手机", "手机", "华为专卖店"); list.add(goods); } solrTemplate.saveBeans(list); //添加集合对象,调用saveBeans();添加普通对象类型数据,使用saveBean(); solrTemplate.commit(); //提交 }
还能够批量插入数据,或者分页查询
@Test public void testPageQuery() { Query query = new SimpleQuery("*:*"); query.setOffset(20); //开始索引(默认0) query.setRows(20); //每页记录数(默认10) ScoredPage<Goods> page = solrTemplate.queryForPage(query, Goods.class); System.out.println("总记录数:" + page.getTotalElements()); List<Goods> list = page.getContent(); }
注意要在web.xml加一个过滤,否则注入不了solrTemplate这个bean
spring-solr.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:solr="http://www.springframework.org/schema/data/solr" xsi:schemaLocation="http://www.springframework.org/schema/data/solr http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- solr服务器地址 --> <solr:solr-server id="solrServer" url="http://127.0.0.1:8080/solr/new_core"/> <!-- solr模板,使用solr模板可对索引库进行CRUD的操做 --> <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate"> <constructor-arg ref="solrServer"/> </bean> </beans>
就是加载一个solr的模板
SolrUtil.java
把数据库的数据库批量加入
@Component public class SolrUtil { @Autowired private GoodsMapper goodsMapper; @Autowired private SolrTemplate solrTemplate; /** * 实现将数据库中的数据批量导入到Solr索引库中 */ public void importGoodsData() { List<Goods> list = goodsMapper.findAll(); System.out.println("====商品列表===="); for (Goods goods : list) { System.out.println(goods.getTitle()); } solrTemplate.saveBeans(list); solrTemplate.commit(); //提交 System.out.println("====结束===="); } public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring*.xml"); SolrUtil solrUtil = (SolrUtil) context.getBean("solrUtil"); solrUtil.importGoodsData(); } }
这样就把数据加入索引库中。
实体类有一个Field标识这个实体字段在索引库里的名称
@Field private Long id; //商品ID @Field("item_title") private String title; //商品标题 @Field("item_price") private BigDecimal price; //商品价格 @Field("item_image") private String image; //商品图片 @Field("item_category") private String category; //商品类别 @Field("item_brand") private String brand; //商品品牌 @Field("item_seller") private String seller; //商品卖家
最后,搜索功能的实现
按价格查找
//按价格区间查询 if (searchMap.get("price") != null) { if (!searchMap.get("price").equals("")) { String[] price = ((String) searchMap.get("price")).split("-"); if (!price[0].equals("0")) { //若是起点区间不等于0 Criteria filterCriteria = new Criteria("item_price").greaterThanEqual(price[0]); FilterQuery filterQuery = new SimpleFilterQuery(filterCriteria); query.addFilterQuery(filterQuery); } if (!price[1].equals("*")) { //若是区间重点不等于* Criteria filterCriteria = new Criteria("item_price").lessThanEqual(price[1]); FilterQuery filterQuery = new SimpleFilterQuery(filterCriteria); query.addFilterQuery(filterQuery); } } }