雖然支持任何編程語言的能力具備很大的市場價值,你可能感興趣的問題是:我如何將Solr的應用集成到Spring中?能夠,Spring Data Solr就是爲了方便Solr的開發所研製的一個框架,其底層是對SolrJ(官方API)的封裝。java
(1) 建立maven工程,pom.xml中引入依賴spring
<dependencies>apache <dependency>編程 <groupId>org.springframework.data</groupId>服務器 <artifactId>spring-data-solr</artifactId>app <version>1.5.5.RELEASE</version>框架 </dependency> maven <dependency>編程語言 <groupId>org.springframework</groupId>ide <artifactId>spring-test</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> |
(2)在src/main/resources下建立 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.188.128:8080/solr" /> <!-- solr模板,使用solr模板可對索引庫進行CRUD的操做 --> <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate"> <constructor-arg ref="solrServer" /> </bean> </beans> |
建立 com.offfcn.pojo 包,將優樂選的TbItem實體類拷入本工程 ,屬性使用@Field註解標識 。 若是屬性與配置文件定義的域名稱不一致,須要在註解中指定域名稱。
package com.offfcn.pojo;
|
建立測試類TestTemplate.java
@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(2000)); 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()); List<TbItem> list = page.getContent(); showList(list); } //顯示記錄數據 private void showList(List<TbItem> list){ for(TbItem item:list){ System.out.println(item.getTitle() +item.getPrice()); } } |
Criteria 用於對條件的封裝:
@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(); } |