Spring Data Solr是为了方便Solr的开发所研制的一个框架,其底层是对SolrJ(官方API)的封装。
maven的pom文件引入jar包:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>1.5.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
创建xml配置文件(applicationContext-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- solr服务器地址 -->
<solr:solr-server id="solrServer" url="http://127.0.0.1:8080/solr-4.10.3" />
<!-- solr模板,使用solr模板可对索引库进行CRUD的操作 -->
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
</bean>
</beans>
加入pojo文件,注意动态solr域注解使用:
@Field("item_seller")
private String seller;
@Dynamic
@Field("item_spec_*")
private Map<String,String> specMap;
public Map<String, String> getSpecMap() {
return specMap;
}
public void setSpecMap(Map<String, String> specMap) {
this.specMap = specMap;
}
用junit进行简单功能测试:
package cn.itcast.test;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.SolrDataQuery;
import org.springframework.data.solr.core.query.result.ScoredPage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.itcast.pojo.TbItem;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-solr.xml")
public class TestTemplate {
@Autowired
private SolrTemplate solrTemplate;
//添加数据
@Test
public void testAdd() {
TbItem item = new TbItem();
item.setId(1L);
item.setTitle("华为V10");
item.setCategory("手机");
item.setBrand("华为");
item.setSeller("华为旗舰店");
item.setGoodsId(10L);
item.setPrice(new BigDecimal(300.00));
solrTemplate.saveBean(item);
solrTemplate.commit();
}
//根据id进行查找
@Test
public void findById() {
TbItem item = solrTemplate.getById(1L, TbItem.class);
System.out.println(item.getTitle());
}
//删除数据
@Test
public void deleteById() {
solrTemplate.deleteById("1");
solrTemplate.commit();
}
//批量添加数据
@Test
public void testAddList() {
List list=new ArrayList();
for(int i=0;i<100;i++) {
TbItem item = new TbItem();
item.setId(i+1L);
item.setTitle("华为V"+i);
item.setCategory("手机");
item.setBrand("华为"+i);
item.setSeller("华为旗舰店");
item.setGoodsId(10L);
item.setPrice(new BigDecimal(300.00+i));
list.add(item);
}
solrTemplate.saveBeans(list);
solrTemplate.commit();
}
//分页查询
@Test
public void testPageQuery() {
Query query=new SimpleQuery("*:*");
query.setOffset(20);//开始索引
query.setRows(20);//每页记录数
ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);
for(TbItem item:page.getContent()) {
System.out.println(item.getTitle()+item.getPrice()+item.getBrand());
}
System.out.println("总记录数:"+page.getTotalElements());
System.out.println("总页数:"+page.getTotalPages());
}
//条件查询
@Test
public void testPageQueryMutil() {
Query query=new SimpleQuery("*:*");
Criteria criteria=new Criteria("item_category").contains("手机");
criteria=criteria.and("item_brand").contains("2");
query.addCriteria(criteria);
//query.setOffset(20);//开始索引
//query.setRows(20);//每页记录数
ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);
for(TbItem item:page.getContent()) {
System.out.println(item.getTitle()+item.getPrice()+item.getBrand());
}
System.out.println("总记录数:"+page.getTotalElements());
System.out.println("总页数:"+page.getTotalPages());
}
//删除
@Test
public void deleteAll() {
Query query=new SimpleQuery("*:*");
solrTemplate.delete(query);
solrTemplate.commit();
}
}