Spring Data Solr简介
虽然支持任何编程语言的能力具有很大的市场价值,你可能感兴趣的问题是:我如何将Solr的应用集成到Spring中?可以,Spring Data Solr就是为了方便Solr的开发所研制的一个框架,其底层是对SolrJ(官方API)的封装。
搭建工程
准备配置文件
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://192.168.157.111:8080/solr" /> <!-- solr模板,使用solr模板可对索引库进行CRUD的操作 --> <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate"> <constructor-arg ref="solrServer" /> </bean> </beans>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hfl.demo</groupId> <artifactId>springdatasolr-demo</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <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> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> </dependency> </dependencies> </project>
修改schama自定义域
/opt/mod/solrhome/collection1/conf下的schama.xml <!--自定义域 --> <field name="item_goodsid" type="long" indexed="true" stored="true"/> <field name="item_title" type="text_ik" indexed="true" stored="true"/> <field name="item_price" type="double" indexed="true" stored="true"/> <field name="item_image" type="string" indexed="false" stored="true" /> <field name="item_category" type="string" indexed="true" stored="true" /> <field name="item_seller" type="text_ik" indexed="true" stored="true" /> <field name="item_brand" type="string" indexed="true" stored="true" />
pojo映射的类
import org.apache.solr.client.solrj.beans.Field; import java.io.Serializable; import java.math.BigDecimal; public class TbItem implements Serializable { @Field private Long id; @Field("item_title") private String title; @Field("item_price") private BigDecimal price; @Field("item_image") private String image; @Field("item_goodsid") private Long goodsId; @Field("item_category") private String category; @Field("item_brand") private String brand; @Field("item_seller") private String seller; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public Long getGoodsId() { return goodsId; } public void setGoodsId(Long goodsId) { this.goodsId = goodsId; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getSeller() { return seller; } public void setSeller(String seller) { this.seller = seller; } }
常用操作测试
@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.setBrand("华为"); item.setCategory("手机"); item.setGoodsId(1L); item.setSeller("华为2号专卖店"); item.setTitle("华为Mate9"); item.setPrice(new BigDecimal(2001)); solrTemplate.saveBean(item); solrTemplate.commit(); } /** * 增加(修改) */ @Test public void testFindOne() { TbItem item = solrTemplate.getById(1, TbItem.class); System.out.println(item.getTitle()); } /** * 按主键删除 */ @Test public void testDelete() { solrTemplate.deleteById("1"); solrTemplate.commit(); } /** * 插入100条测试数据 */ @Test public void testAddList() { List<TbItem> list = new ArrayList(); for (int i = 0; i < 100; i++) { TbItem item = new TbItem(); item.setId(i + 1L); item.setBrand("华为"); item.setCategory("手机"); item.setGoodsId(1L); item.setSeller("华为2号专卖店"); item.setTitle("华为Mate" + i); item.setPrice(new BigDecimal(2000 + i)); list.add(item); } solrTemplate.saveBeans(list); solrTemplate.commit(); } /** * 分页查询测试 */ @Test public void testPageQuery() { Query query = new SimpleQuery("*:*"); query.setOffset(20);//开始索引(默认0) query.setRows(20);//每页记录数(默认10) ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class); System.out.println("总记录数:" + page.getTotalElements()); System.out.println("总页数数:" + page.getTotalPages()); List<TbItem> list = page.getContent(); showList(list); } /** * 显示记录数据 * * @param list */ private void showList(List<TbItem> list) { for (TbItem item : list) { System.out.println(item.getTitle() + item.getPrice()); } } /** * 条件查询 */ @Test public void testPageQueryMutil() { Query query = new SimpleQuery("*:*"); Criteria criteria = new Criteria("item_title").contains("2"); criteria = criteria.and("item_title").contains("5"); query.addCriteria(criteria); //query.setOffset(20);//开始索引(默认0) //query.setRows(20);//每页记录数(默认10) ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class); System.out.println("总记录数:" + page.getTotalElements()); List<TbItem> list = page.getContent(); showList(list); } /** * 删除全部数据 */ @Test public void testDeleteAll() { Query query = new SimpleQuery("*:*"); solrTemplate.delete(query); solrTemplate.commit(); } }
代码地址
https://github.com/hufanglei/pinyou/tree/sping-data-solr-demo
完