Solr使用入門指南

因爲搜索引擎功能在門戶社區中對提升用戶體驗有着重在門戶社區中涉及大量須要搜索引擎的功能需求,目前在實現搜索引擎的方案上有集中方案可供選擇:php

1. 基於Lucene本身進行封裝實現站內搜索。工做量及擴展性都較大,不採用。html

2. 調用Google、Baidu的API實現站內搜索。同第三方搜索引擎綁定太死,沒法知足後期業務擴展須要,暫時不採用。java

3. 基於Compass+Lucene實現站內搜索。適合於對數據庫驅動的應用數據進行索引,尤爲是替代傳統的like ‘%expression%’來實現對varchar或clob等字段的索引,對於實現站內搜索是一種值得采納的方案。但在分佈式處理、接口封裝上尚須要本身進行必定程度的封裝,暫時不採用。web

4. 基於Solr實現站內搜索。封裝及擴展性較好,提供了較爲完備的解決方案,所以在門戶社區中採用此方案,後期加入Compass方案。shell

 

一、 Solr簡介數據庫

Solr是一個基於Lucene的Java搜索引擎服務器。Solr 提供了層面搜索、命中醒目顯示而且支持多種輸出格式(包括 XML/XSLT 和 JSON 格式)。它易於安裝和配置,並且附帶了一個基於 HTTP 的管理界面。Solr已經在衆多大型的網站中使用,較爲成熟和穩定。Solr 包裝並擴展了 Lucene,因此Solr的基本上沿用了Lucene的相關術語。更重要的是,Solr 建立的索引與 Lucene 搜索引擎庫徹底兼容。經過對Solr 進行適當的配置,某些狀況下可能須要進行編碼,Solr 能夠閱讀和使用構建到其餘 Lucene 應用程序中的索引。此外,不少 Lucene 工具(如Nutch、 Luke)也可使用Solr 建立的索引。express


二、 Tomcat下Solr安裝配置
因爲Solr基於java開發,所以Solr在windows及Linux都能較好部署使用,但因爲Solr提供了一些用於測試及管理、維護較爲方便的shell腳本,所以在生產部署時候建議安裝在Linux上,測試時候能夠在windows使用。apache

 

下面以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的理解和配置,主要有三種windows

 

基於當前路徑的方式 
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


三、 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

四、 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"

五、Solr查詢條件參數說明
參數     描述     示例
q    

Solr 中用來搜索的查詢。能夠經過追加一個分號和已索引且未進行斷詞的字段的名稱來包含排序信息。默認的排序是 score desc,指按記分降序排序。    
q=myField:Java AND otherField:developerWorks; date asc
此查詢搜索指定的兩個字段並根據一個日期字段對結果進行排序。

 

start     將初始偏移量指定到結果集中。可用於對結果進行分頁。默認值爲 0。     
start=15
返回從第 15 個結果開始的結果。

 

rows     返回文檔的最大數目。默認值爲 10。     rows=25

 

fq     提供一個可選的篩選器查詢。查詢結果被限制爲僅搜索篩選器查詢返回的結果。篩選過的查詢由 Solr 進行緩存。它們對提升複雜查詢的速度很是有用。
任何能夠用 q 參數傳遞的有效查詢,排序信息除外。

 

hl     當 hl=true 時,在查詢響應中醒目顯示片斷。默認爲 false。參看醒目顯示參數上的 Solr Wiki 部分能夠查看更多選項     hl=true

 

fl     做爲逗號分隔的列表指定文檔結果中應返回的 Field 集。默認爲 「*」,指全部的字段。「score」 指還應返回記分。     
*,score

其中關於Solr查詢相關的參數詳細的信息請參看:
http://wiki.apache.org/solr/CommonQueryParameters

Solr的查詢條件參數q的格式與Lucene相同,具體參看:
http://lucene.apache.org/java/docs/queryparsersyntax.html

六、 在門戶社區中solr使用模式 
在門戶社區中須要使用solr,可採用以下模式:

 

對原有系統已有的數據或須要索引的數據量較大的狀況 
直接採用經過http方式調用solr的接口方式,效率較差,採用solr自己對csv 的支持(http://wiki.apache.org/solr/UpdateCSV
),將數據導出爲csv格式,而後調用solr的csv接口http://localhost:8080/solr/update/csv

對系統新增的數據 
先將須要索引查詢的數據組裝成xml格式,而後使用httpclient 將數據提交到solr 的http接口,例如   
http://localhost:8080/solr/update

也能夠參考post.jar中的SimplePostTool的實現。
http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/util/SimplePostTool.java?view=co

 

中文分詞
採用庖丁解牛做爲solr(Lucene)缺省的中文分詞方案
項目庫:http://code.google.com/p/paoding/

Google groups:http://groups.google.com/group/paoding
Javaeye的groups:http://analysis.group.javaeye.com/


與nutch的集成使用
http://blog.foofactory.fi/2007/02/online-indexing-integrating-nutch-with.html

嵌入式Solr
http://wiki.apache.org/solr/Solrj#EmbeddedSolrServer

分佈式索引
http://wiki.apache.org/solr/CollectionDistribution

七、參考資料 http://wiki.apache.org/solr/ http://www.ibm.com/developerworks/cn/java/j-solr1/ http://www.ibm.com/developerworks/cn/java/j-solr2/ http://www.xml.com/pub/a/2006/08/09/solr-indexing-xml-with-lucene-andrest.html?page=1 http://lucene.apache.org/java/docs/queryparsersyntax.html http://www.blogjava.net/RongHao/archive/2007/11/06/158621.html

相關文章
相關標籤/搜索