最近準備用Solr搭建項目,因此對其做了一些瞭解,我採用的是Solr4.4版本;這個版本的Solr相對於之前的版本改變很大,這裏記一下本身安裝與配置的過程。html
網上不少關於Solr的教程都很老了,不少教程竟然仍是基於Solr1.3或者1.4版本在講解,有些資料的內容看了再對比當前版本會不知所云。java
儘可能多看官方提供的wiki文檔:http://wiki.apache.org/solr/web
這裏貼一段關於solr的簡介:apache
Solr是一個基於Lucene的Java搜索引擎服務器。Solr 提供了層面搜索、命中醒目顯示而且支持多種輸出格式(包括 XML/XSLT 和 JSON 格式)。它易於安裝和配置,並且附帶了一個基於 HTTP 的管理界面。Solr已經在衆多大型的網站中使用,較爲成熟和穩定。Solr 包裝並擴展了 Lucene,因此Solr的基本上沿用了Lucene的相關術語。更重要的是,Solr 建立的索引與 Lucene 搜索引擎庫徹底兼容。經過對Solr 進行適當的配置,某些狀況下可能須要進行編碼,Solr 能夠閱讀和使用構建到其餘 Lucene 應用程序中的索引。此外,不少 Lucene 工具(如Nutch、 Luke)也可使用Solr 建立的索引。bootstrap
官方網址:http://lucene.apache.org/solr/瀏覽器
下載列表:http://www.apache.org/dyn/closer.cgi/lucene/solr/4.4.0tomcat
將下載獲得的solr-4.4.0.zip壓縮包解壓,會看到以下目錄結構:服務器
其中對咱們有用的是example和dist目錄中的內容。app
首先將exmaple/webapps中的solr.war包解壓開復制到Tomcat的webapps中,或者直接將solr.war包直接複製到Tomcat的webapps中,而後啓動Tomcat使其解壓開再將war包刪除。這時若是啓動Tomcat是會報錯的,由於咱們沒有設置solr_home(也就是索引和配置文件所在的目錄)。webapp
下面配置一下:
solr_home的設置有好幾種方式,百度一下就能夠了,這裏我採用的是在solr應用的web.xml文件中配置的方式。打開solr/WEB-INF/web.xml文件,找到以下內容:
這段內容初始時是被註釋了的,解開註釋後將<env-entry-value>節點內容改成本身磁盤上某個位置便可,這裏我採用的是相對位置,絕對位置固然也是能夠的。
配置完了還須要在配置的路徑處放置一個文件夾做爲SOLR_HOME(solr的example中已經提供),咱們將example/solr目錄拷貝到solr項目目錄下(這裏是%TOMCAT_HOME%/webapps/solr/),並重命名爲solr_home(根據本身的配置決定)。
這時若是啓動Tomcat(經過調用startup.sh腳本),仍然沒法正常啓動,並且錯誤只有幾行,以下所示:
給出的錯誤信息不明不白,這時若是咱們換一種方式來啓動Tomcat就會發現錯誤輸出了,在命令行將當前目錄切換到%TOMCAT_HOME%/bin,執行以下的命令:
這樣能夠看到命令行輸出以下的異常信息(省略部份內容):
很顯然,提示咱們找不到日誌相關的jar包,還提示咱們去看solr的相關wiki連接:http://wiki.apache.org/solr/SolrLogging
到該Wiki頁面中咱們會找到以下幾段文字說明:
These versions do not include any logging jars in the WAR file. They must be provided separately. The Solr example for these versions includes jars (in the jetty lib/ext directory) that set up SLF4J with a binding to the Apache log4j library.
To get the same logging setup in another container (Tomcat for example) as with the example Jetty server, you need to do the following
Copy the jars from solr/example/lib/ext into your container's main lib directory. These jars will set up SLF4J and log4j.
Exactly where this lib directory is highly variable. For a Debian or Ubuntu server using the Tomcat package available from the OS vendor, this is likely to be/usr/share/tomcat6/lib or/usr/share/tomcat7/lib.
Copy the logging config from solr/example/resources/log4j.properties into a location on the classpath. Usually you can use the same location as the jar files above. Edit the configuration file for your preferred log destination.
Optionally, if you did not place log4j.properties on the classpath, set java option-Dlog4j.configuration=file:///path/to/log4j.properties
上面解釋了在新版本的solr中日誌輸出方面作了什麼變更,以及如何解決。
首先複製example/lib/ext目錄中的jar包到項目的classpath下,這裏的example就是前面解壓solr壓縮包獲得的,solr應用war包也是從該目錄下獲得的;咱們能夠將這些jar包放到%TOMCAT_HOME%/lib下,也能夠將它們放到項目的lib下(在個人電腦上是/opt/tomcat-6.0.37/webapps/solr/WEB-INF/lib);
而後將example/resources/log4j.properties也拷到classpath(我在/opt/tomcat-6.0.37/webapps/solr/目錄下新建了一個classes目錄,放log4j.properties放了進去);
而後再啓動Tomcat,這時就沒有異常拋出了,solr成功啓動,咱們經過瀏覽器進行訪問,你將會看到以下畫面:
使用全文檢索,中文分詞是離不開的,這裏我採用的是mmseg4j分詞器。mmseg4j分詞器內置了對solr的支持,最新版本可支持4.X版本的sorl,使用起來非常方便。
GoogleCode地址:http://code.google.com/p/mmseg4j/
使用mmseg4j中文分詞器,首先須要在schema.xml文件中配置一個fieldType節點(其它的中文分詞器也相似),以下所示:
而後就能夠在field節點中引用該filedType了,定義示例filed節點以下:
而後在瀏覽器裏試一下效果:
下面總結一下Solr4.4.0的安裝與配置步驟: