共計 6652 個字符,預計需要花費 17 分鐘才能閱讀完成。
這篇文章主要介紹“Solr 使用方法是什么”,在日常操作中,相信很多人在 Solr 使用方法是什么問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Solr 使用方法是什么”的疑惑有所幫助!接下來,請跟著丸趣 TV 小編一起來學習吧!
由于搜索引擎功能在門戶社區中對提高用戶體驗有著重在門戶社區中涉及大量需要搜索引擎的功能需求,目前在實現搜索引擎的方案上有集中方案可供選擇:
1. 基于 Lucene 自己進行封裝實現站內搜索。工作量及擴展性都較大,不采用。
2. 調用 Google、Baidu 的 API 實現站內搜索。同第三方搜索引擎綁定太死,無法滿足后期業務擴展需要,暫時不采用。
3. 基于 Compass+Lucene 實現站內搜索。適合于對數據庫驅動的應用數據進行索引,尤其是替代傳統的 like‘%expression%’來實現對 varchar 或 clob 等字段的索引,對于實現站內搜索是一種值得采納的方案。但在分布式處理、接口封裝上尚需要自己進行一定程度的封裝,暫時不采用。
4. 基于 Solr 實現站內搜索。封裝及擴展性較好,提供了較為完備的解決方案,因此在門戶社區中采用此方案,后期加入 Compass 方案。
1、 Solr 簡介
Solr 是一個基于 Lucene 的 Java 搜索引擎服務器。Solr 提供了層面搜索、命中醒目顯示并且支持多種輸出格式(包括 XML/XSLT 和 JSON 格式)。它易于安裝和配置,而且附帶了一個基于 HTTP 的管理界面。Solr 已經在眾多大型的網站中使用,較為成熟和穩定。Solr 包裝并擴展了 Lucene,所以 Solr 的基本上沿用了 Lucene 的相關術語。更重要的是,Solr 創建的索引與 Lucene 搜索引擎庫完全兼容。通過對 Solr 進行適當的配置,某些情況下可能需要進行編碼,Solr 可以閱讀和使用構建到其他 Lucene 應用程序中的索引。此外,很多 Lucene 工具(如 Nutch、Luke)也可以使用 Solr 創建的索引。
2、Tomcat 下 Solr 安裝配置
由于 Solr 基于 java 開發,因此 Solr 在 windows 及 Linux 都能較好部署使用,但由于 Solr 提供了一些用于測試及管理、維護較為方便的 shell 腳本,因此在生產部署時候建議安裝在 Linux 上,測試時候可以在 windows 使用。
下面以 Linux 下安裝配置 Solr 進行說明,windows 與此類似。
wget http://apache.mirror.phpchina.com/tomcat/tomcat-6/v6.0.16/bin/apache-tomcat-6.0.16.zip
unzip apache-tomcat-6.0.16.zip
mv apache-tomcat-6.0.16 /opt/tomcat
chmod 755 /opt/tomcat/bin/*
wget http://apache.mirror.phpchina.com/lucene/solr/1.2/apache-solr-1.2.0.tgz
tar zxvf apache-solr-1.2.0.tgz
Solr 的安裝配置最為麻煩的是對 solr.solr.home 的理解和配置,主要有三種
基于當前路徑的方式
cp apache-solr-1.2.0/dist/apache-solr-1.2.0.war /opt/tomcat/webapps/solr.war
mkdir /opt/solr-tomcat
cp -r apache-solr-1.2.0/example/solr/ /opt/solr-tomcat/
cd /opt/solr-tomcat
/opt/tomcat/bin/startup.sh
由于在此種情況下(沒有設定 solr.solr.home 環境變量或 JNDI 的情況下),Solr 查找./solr,因此在啟動時候需要切換到 /opt/solr-tomcat
基于環境變量 solr.solr.home
在當前用戶的環境變量中(.bash_profile)或在 /opt/tomcat/catalina.sh 中添加如下環境變量
export JAVA_OPTS= $JAVA_OPTS -Dsolr.solr.home=/opt/solr-tomcat/solr
基于 JNDI 配置
mkdir –p /opt/tomcat/conf/Catalina/localhost
touch /opt/tomcat/conf/Catalina/localhost/solr.xml,內容如下:
Context docBase= /opt/tomcat/webapps/solr.war debug= 0 crossContext= true
Environment name= solr/home type= java.lang.String value= /opt/solr-tomcat/solr override= true /
/Context
訪問 solr 管理界面 http://ip:port/solr
3、 Solr 原理
Solr 對外提供標準的 http 接口來實現對數據的索引的增加、刪除、修改、查詢。在 Solr 中,用戶通過向部署在 servlet 容器中的 Solr Web 應用程序發送 HTTP 請求來啟動索引和搜索。Solr 接受請求,確定要使用的適當 SolrRequestHandler,然后處理請求。通過 HTTP 以同樣的方式返回響應。默認配置返回 Solr 的標準 XML 響應,也可以配置 Solr 的備用響應格式。
可以向 Solr 索引 servlet 傳遞四個不同的索引請求:
add/update 允許向 Solr 添加文檔或更新文檔。直到提交后才能搜索到這些添加和更新。
commit 告訴 Solr,應該使上次提交以來所做的所有更改都可以搜索到。
optimize 重構 Lucene 的文件以改進搜索性能。索引完成后執行一下優化通常比較好。如果更新比較頻繁,則應該在使用率較低的時候安排優化。一個索引無需優化也可以正常地運行。優化是一個耗時較多的過程。
delete 可以通過 id 或查詢來指定。按 id 刪除將刪除具有指定 id 的文檔;按查詢刪除將刪除查詢返回的所有文檔。
一個典型的 Add 請求報文
add
doc
field name= id TWINX2048-3200PRO /field
field name= name CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail /field
field name= manu Corsair Microsystems Inc. /field
field name= cat electronics /field
field name= cat memory /field
field name= features CAS latency 2, 2-3-3-6 timing, 2.75v, unbuffered, heat-spreader /field
field name= price 185 /field
field name= popularity 5 /field
field name= inStock true /field
/doc
doc
field name= id VS1GB400C3 /field
field name= name CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail /field
field name= manu Corsair Microsystems Inc. /field
field name= cat electronics /field
field name= cat memory /field
field name= price 74.99 /field
field name= popularity 7 /field
field name= inStock true /field
/doc
/add
一個典型的搜索結果報文:
response
lst name= responseHeader
int name= status 0 /int
int name= QTime 6 /int
lst name= params
str name= rows 10 /str
str name= start 0 /str
str name= fl *,score /str
str name= hl true /str
str name= q content: faceted browsing /str
/lst
/lst
result name= response numFound= 1 start= 0 maxScore= 1.058217
doc
float name= score 1.058217 /float
arr name= all
str http://localhost/myBlog/solr-rocks-again.html /str
str Solr is Great /str
str solr,lucene,enterprise,search,greatness /str
str Solr has some really great features, like faceted browsing and replication /str
/arr
arr name= content
str Solr has some really great features, like faceted browsing and replication /str
/arr
date name= creationDate 2007-01-07T05:04:00.000Z /date
arr name= keywords
str solr,lucene,enterprise,search,greatness /str
/arr
int name= rating 8 /int
str name= title Solr is Great /str
str name= url http://localhost/myBlog/solr-rocks-again.html /str
/doc
/result
lst name= highlighting
lst name= http://localhost/myBlog/solr-rocks-again.html
arr name= content
str Solr has some really great features, like em faceted /em
em browsing /em and replication /str
/arr
/lst
/lst
/response
關于 solr 的詳細使用說明,請參考
http://wiki.apache.org/solr/FrontPage
4、 Solr 測試使用
Solr 的安裝包包含了相關的測試樣例,路徑在 apache-solr-1.2.0/example/exampledocs
1. 使用 shell 腳本(curl)測試 Solr 的操作:
cd apache-solr-1.2.0/example/exampledocs
vi post.sh,根據 tomcat 的 ip、port 修改 URL 變量的值 URL=http://localhost:8080/solr/update
./post.sh *.xml #
2. 使用 Solr 的 java 包測試 Solr 的操作:
查看幫助:java -jar post.jar –help
提交測試數據:
java -Durl=http://localhost:8080/solr/update -Ddata=files -jar post.jar *.xml
下面以增加索引字段 liangchuan、url 為例,說明一下 Solr 中索引命令的使用
1) 修改 solr 的 schema,配置需要索引字段的說明:
vi /opt/solr-tomcat/solr/conf/schema.xml , 在 fields 中增加如下內容
field name= liangchuan type= string indexed= true stored= true /
field name= url type= string indexed= true stored= true /
2) 創建增加索引請求的 xml 測試文件
touch /root/apache-solr-1.2.0/example/exampledocs/liangchuan.xml, 內容如下:
add
doc
field name= id liangchuan000 /field
field name= name Solr, the Enterprise Search Server /field
field name= manu Apache Software Foundation /field
field name= liangchuan liangchuan s solr hello,world test /field
field name= url http://www.google.com /field
/doc
/add
3) 提交索引請求
cd apache-solr-1.2.0/example/exampledocs
./post.sh liangchuan.xml
4) 查詢
通過 solr 的管理員界面 http://localhost:8080/solr/admin 查詢
或通過 curl 測試:
export URL= http://localhost:8080/solr/select/
curl $URL?indent=on q=liangchuan fl=*,score
5、Solr 查詢條件參數說明
參數 描述 示例
q
Solr 中用來搜索的查詢。可以通過追加一個分號和已索引且未進行斷詞的字段的名稱來包含排序信息。默認的排序是 score desc,指按記分降序排序。
q=myField:Java AND otherField:developerWorks; date asc
此查詢搜索指定的兩個字段并根據一個日期字段對結果進行排序。
start 將初始偏移量指定到結果集中??捎糜趯Y果進行分頁。默認值為 0。
start=15
返回從第 15 個結果開始的結果。
rows 返回文檔的最大數目。默認值為 10。 rows=25
fq 提供一個可選的篩選器查詢。查詢結果被限制為僅搜索篩選器查詢返回的結果。篩選過的查詢由 Solr 進行緩存。它們對提高復雜查詢的速度非常有用。
任何可以用 q 參數傳遞的有效查詢,排序信息除外。
hl 當 hl=true 時,在查詢響應中醒目顯示片段。默認為 false。參看醒目顯示參數上的 Solr Wiki 部分可以查看更多選項 hl=true
fl 作為逗號分隔的列表指定文檔結果中應返回的 Field 集。默認為“*”,指所有的字段?!皊core”指還應返回記分。
*,score
Solr 的查詢條件參數 q 的格式與 Lucene 相同
6、在門戶社區中 solr 使用模式
在門戶社區中需要使用 solr,可采用如下模式:
對原有系統已有的數據或需要索引的數據量較大的情況
直接采用通過 http 方式調用 solr 的接口方式,效率較差,采用 solr 本身對 csv 的支持,將數據導出為 csv 格式,然后調用 solr 的 csv 接口 http://localhost:8080/solr/update/csv
對系統新增的數據
先將需要索引查詢的數據組裝成 xml 格式,然后使用 httpclient 將數據提交到 solr 的 http 接口,例如
http://localhost:8080/solr/update
到此,關于“Solr 使用方法是什么”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注丸趣 TV 網站,丸趣 TV 小編會繼續努力為大家帶來更多實用的文章!