Solr從搭建到入門
作者丨非科班的科班來源丨非科班的科班
1.Solr安裝與配置
1.1?Solr簡(jiǎn)介
大多數(shù)搜索引擎應(yīng)用都必須具有某種搜索功能,問題是搜索功能往往是巨大的資源消耗并且它們由于沉重的數(shù)據(jù)庫(kù)加載而拖垮你的應(yīng)用的性能。Solr是一個(gè)開源搜索平臺(tái),用于構(gòu)建搜索應(yīng)用程序。它建立在Lucene(全文搜索引擎)之上。Solr是企業(yè)級(jí)的,快速的和高度可擴(kuò)展的。使用Solr構(gòu)建的應(yīng)用程序非常復(fù)雜,可提供高性能。Solr可以和Hadoop一起使用。由于Hadoop處理大量數(shù)據(jù),Solr幫助我們從這么大的源中找到所需的信息。不僅限于搜索,Solr也可以用于存儲(chǔ)目的。像其他NoSQL數(shù)據(jù)庫(kù)一樣,它是一種非關(guān)系數(shù)據(jù)存儲(chǔ)和處理技術(shù)。Solr是一個(gè)可擴(kuò)展的,可部署,搜索/存儲(chǔ)引擎,優(yōu)化搜索大量以文本為中心的數(shù)據(jù)。1.2 Solr安裝
1:下載 Tomcat,解壓Tomcat壓縮包即可。2:解壓 solr。
3:把 solr 下的dist目錄solr-4.10.3.war部署到 Tomcat\webapps下(去掉版本號(hào))。

4:啟動(dòng) Tomcat解壓縮 war 包,啟動(dòng)tomcat就是直接來到tomcat'的bin目錄下,雙擊startup,就可啟動(dòng)
5:把solr下example/lib/ext 目錄下的所有的 jar 包,添加到 solr 的工程中(\WEB-INF\lib目錄下)。

6:創(chuàng)建一個(gè) solrhome 。solr 下的/example/solr 目錄就是一個(gè) solrhome。復(fù)制此目錄到D盤改名為solrhome

7:關(guān)聯(lián) solr 及 solrhome。需要修改 solr 工程的 web.xml 文件。

????<env-entry>8:啟動(dòng) Tomcat,在瀏覽器訪問http://IP:8080/solr/
???????<env-entry-name>solr/homeenv-entry-name>
???????<env-entry-value>d:\solrhomeenv-entry-value>
???????<env-entry-type>java.lang.Stringenv-entry-type>
????env-entry>

到這里solr就安裝成功了,接下來是配置分詞器。
1.3中文分析器IK Analyzer
1.3.1 IK Analyzer簡(jiǎn)介
IK Analyzer 是一個(gè)開源的,基亍 java 語(yǔ)言開發(fā)的輕量級(jí)的中文分詞工具包。從 2006年 12 月推出 1.0 版開始, IKAnalyzer 已經(jīng)推出了 4 個(gè)大版本。最初,它是以開源項(xiàng)目Luence 為應(yīng)用主體的,結(jié)合詞典分詞和文法分析算法的中文分詞組件。從 3.0 版本開始,IK 發(fā)展為面向 Java 的公用分詞組件,獨(dú)立亍 Lucene 項(xiàng)目,同時(shí)提供了對(duì) Lucene 的默認(rèn)優(yōu)化實(shí)現(xiàn)。在 2012 版本中,IK 實(shí)現(xiàn)了簡(jiǎn)單的分詞歧義排除算法,標(biāo)志著 IK 分詞器從單純的詞典分詞向模擬語(yǔ)義分詞衍化。1.3.2 IK Analyzer配置
1、把IKAnalyzer2012FF_u1.jar 添加到 solr 工程的 lib 目錄下
2、創(chuàng)建WEB-INF/classes文件夾 ?把擴(kuò)展詞典、停用詞詞典、配置文件放到 solr 工程的 WEB-INF/classes 目錄下。

3、修改 Solrhome 的 schema.xml 文件,配置一個(gè) FieldType,使用 IKAnalyzer

"text_ik" ?class="solr.TextField">
?????class ="org.wltea.analyzer.lucene.IKAnalyzer"/>
1.4配置域
域相當(dāng)于數(shù)據(jù)庫(kù)的表字段,用戶存放數(shù)據(jù),因此用戶根據(jù)業(yè)務(wù)需要去定義相關(guān)的Field(域),一般來說,每一種對(duì)應(yīng)著一種數(shù)據(jù),用戶對(duì)同一種數(shù)據(jù)進(jìn)行相同的操作。域的常用屬性:
? ? ?name:指定域的名稱
? ? ?type:指定域的類型
? ? ?indexed:是否索引
? ? ?stored:是否存儲(chǔ)
? ? ?required:是否必須
? ? ?multiValued:是否多值
1.4.1域
修改solrhome的schema.xml 文件 ?設(shè)置業(yè)務(wù)系統(tǒng) Field
????"item_goodsid" ?type="long"?indexed="true"?stored="true"/>
????"item_title" ?type="text_ik"?indexed="true"?stored="true"/>
????"item_price" ?type="double"?indexed="true"?stored="true"/>
????"item_image" ?type="string"?indexed="false"?stored="true"?/>
????"item_category" ?type="string"?indexed="true"?stored="true"?/>
????"item_seller" ?type="text_ik"?indexed="true"?stored="true"?/>
????"item_brand" ?type="string"?indexed="true"?stored="true"?/>
1.4.2復(fù)制域
復(fù)制域的作用在于將某一個(gè)Field中的數(shù)據(jù)復(fù)制到另一個(gè)域中"item_keywords" ?type="text_ik"?indexed="true"?stored="false"?multiValued="true"/>source ="item_title"?dest="item_keywords"/>source ="item_category"?dest="item_keywords"/>source ="item_seller"?dest="item_keywords"/>source ="item_brand"?dest="item_keywords"/>
1.4.3動(dòng)態(tài)域
當(dāng)我們需要?jiǎng)討B(tài)擴(kuò)充字段時(shí),我們需要使用動(dòng)態(tài)域。對(duì)于品優(yōu)購(gòu),規(guī)格的值是不確定的,所以我們需要使用動(dòng)態(tài)域來實(shí)現(xiàn)。需要實(shí)現(xiàn)的效果如下:? ?

配置:
"item_spec_*" ?type="string"?indexed="true"?stored="true"?/>????
2.Spring Data Solr實(shí)戰(zhàn)
2.1 Spring Data Solr簡(jiǎn)介
雖然支持任何編程語(yǔ)言的能力具有很大的市場(chǎng)價(jià)值,你可能感興趣的問題是:我如何將Solr的應(yīng)用集成到Spring中?可以,Spring Data Solr就是為了方便Solr的開發(fā)所研制的一個(gè)框架,其底層是對(duì)SolrJ(官方API)的封裝。2.2 Spring Data Solr入門
2.2.1 搭建工程
(1)創(chuàng)建maven工程,pom.xml中引入依賴<dependencies>(2)在src/main/resources下創(chuàng)建 ?applicationContext-solr.xml
????<dependency>
????????<groupId>org.springframework.datagroupId>
????????<artifactId>spring-data-solrartifactId>
????????<version>1.5.5.RELEASEversion>
????dependency>?
????<dependency>
????????<groupId>org.springframeworkgroupId>
????????<artifactId>spring-testartifactId>
????????<version>4.2.4.RELEASEversion>
????dependency>
????<dependency>
????????<groupId>junitgroupId>
????????<artifactId>junitartifactId>
????????<version>4.9version>
????dependency>
??dependencies>
<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-server?id="solrServer"?url="http://127.0.0.1:8080/solr"?/>
????
????<bean?id="solrTemplate"?class="org.springframework.data.solr.core.SolrTemplate">
????????<constructor-arg?ref="solrServer"?/>
????bean>
beans>
2.2.2 @Field 注解
創(chuàng)建 cn.itcast.pojo 包,將品優(yōu)購(gòu)的TbItem實(shí)體類拷入本工程 ?,屬性使用@Field注解標(biāo)識(shí) 。? 如果屬性與配置文件定義的域名稱不一致,需要在注解中指定域名稱。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;
.......
}
2.2.3 增加(修改)
創(chuàng)建測(cè)試類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("手機(jī)");
????????item.setGoodsId(1L);
????????item.setSeller("華為2號(hào)專賣店");
????????item.setTitle("華為Mate9");
????????item.setPrice(new?BigDecimal(2000));????????
????????solrTemplate.saveBean(item);
????????solrTemplate.commit();
????}
}
2.2.4 按主鍵查詢
????@Test
????public?void?testFindOne(){
????????TbItem?item?=?solrTemplate.getById(1,?TbItem.class);
????????System.out.println(item.getTitle());
????}
2.2.5 按主鍵刪除
@Test
????public?void?testDelete(){
????????solrTemplate.deleteById("1");
????????solrTemplate.commit();
????}
2.2.6 分頁(yè)查詢
首先循環(huán)插入100條測(cè)試數(shù)據(jù)????@Test編寫分頁(yè)查詢測(cè)試代碼:
????public?void?testAddList(){
????????List?list=new?ArrayList();
????????for(int?i=0;i<100;i++){
????????????TbItem?item=new?TbItem();
????????????item.setId(i+1L);
????????????item.setBrand("華為");
????????????item.setCategory("手機(jī)");
????????????item.setGoodsId(1L);
????????????item.setSeller("華為2號(hào)專賣店");
????????????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);//開始索引(默認(rèn)0)
????????query.setRows(20);//每頁(yè)記錄數(shù)(默認(rèn)10)
????????ScoredPage?page?=?solrTemplate.queryForPage(query,?TbItem.class);
????????System.out.println("總記錄數(shù):"+page.getTotalElements());
????????List?list?=?page.getContent();
????????showList(list);
????}???
????//顯示記錄數(shù)據(jù)
????private?void?showList(List?list) {???????
????????for(TbItem?item:list){
????????????System.out.println(item.getTitle()?+item.getPrice());
????????}???????
????}
2.2.7 條件查詢
Criteria 用于對(duì)條件的封裝:@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);//開始索引(默認(rèn)0)
????????//query.setRows(20);//每頁(yè)記錄數(shù)(默認(rèn)10)
????????ScoredPage?page?=?solrTemplate.queryForPage(query,?TbItem.class);
????????System.out.println("總記錄數(shù):"+page.getTotalElements());
????????List?list?=?page.getContent();
????????showList(list);
????}
2.2.8 刪除全部數(shù)據(jù)
@Test
????public?void?testDeleteAll(){
????????Query?query=new?SimpleQuery("*:*");
????????solrTemplate.delete(query);
????????solrTemplate.commit();
????}
近期精彩內(nèi)容推薦:??
?編程語(yǔ)言性能實(shí)測(cè),Go和Python誰(shuí)更牛?

在看點(diǎn)這里
好文分享給更多人↓↓
評(píng)論
圖片
表情
