Solr4.4的安裝與配置


Solr簡介

這裏貼一段關於solr的簡介:

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


下載解壓


官方網址:http://lucene.apache.org/solr/

下載列表:http://www.apache.org/dyn/closer.cgi/lucene/solr/4.4.0

將下載得到的solr-4.4.0.zip壓縮包解壓,會看到如下目錄結構:


其中對我們有用的是example和dist目錄中的內容。


安裝與配置


首先將exmaple/webapps中的solr.war包解壓開復制到Tomcat的webapps中,或者直接將solr.war包直接複製到Tomcat的webapps中,然後啓動Tomcat使其解壓開再將war包刪除。這時如果啓動Tomcat是會報錯的,因爲我們沒有設置solr_home(也就是索引和配置文件所在的目錄)。

下面配置一下:

solr_home的設置有好幾種方式,百度一下就可以了,這裏我採用的是在solr應用的web.xml文件中配置的方式。打開solr/WEB-INF/web.xml文件,找到如下內容:

[html] view plaincopy
  1. <env-entry>  
  2.     <env-entry-name>solr/home</env-entry-name>  
  3.     <env-entry-value>../webapps/solr/solr_home</env-entry-value>  
  4.     <env-entry-type>java.lang.String</env-entry-type>  
  5. </env-entry>  

這段內容初始時是被註釋了的,解開註釋後將<env-entry-value>節點內容改爲自己磁盤上某個位置即可,這裏我採用的是相對位置,絕對位置當然也是可以的。

配置完了還需要在配置的路徑處放置一個文件夾作爲SOLR_HOME(solr的example中已經提供),我們將example/solr目錄拷貝到solr項目目錄下(這裏是%TOMCAT_HOME%/webapps/solr/),並重命名爲solr_home(根據自己的配置決定)。


配置日誌輸出


這時如果啓動Tomcat(通過調用startup.sh腳本),仍然無法正常啓動,而且錯誤只有幾行,如下所示:

[java] view plaincopy
  1. 2013-8-9 15:49:05 org.apache.catalina.core.StandardContext start  
  2. 嚴重: Error filterStart  
  3. 2013-8-9 15:49:05 org.apache.catalina.core.StandardContext start  
  4. 嚴重: Context [/solr] startup failed due to previous errors  

給出的錯誤信息不明不白,這時如果我們換一種方式來啓動Tomcat就會發現錯誤輸出了,在命令行將當前目錄切換到%TOMCAT_HOME%/bin,執行如下的命令:

[java] view plaincopy
  1. java -jar bootstrap.jar  

這樣可以看到命令行輸出如下的異常信息(省略部分內容):

[java] view plaincopy
  1. 嚴重: Exception starting filter SolrRequestFilter  
  2. org.apache.solr.common.SolrException: Could not find necessary SLF4j logging jars. If using Jetty, the SLF4j logging jar  
  3. s need to go in the jetty lib/ext directory. For other containers, the corresponding directory should be used. For more  
  4. information, see: http://wiki.apache.org/solr/SolrLogging  
  5.         at org.apache.solr.servlet.SolrDispatchFilter.<init>(SolrDispatchFilter.java:104)  
  6.         ......  
  7.         at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)  
  8.         at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)  
  9. Caused by: java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory  
  10.         at org.apache.solr.servlet.SolrDispatchFilter.<init>(SolrDispatchFilter.java:102)  
  11.         ... 33 more  
  12. Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory  
  13.         at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)  
  14.         at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)  
  15.         ... 34 more  
  16. 2013-8-9 15:38:59 org.apache.catalina.core.StandardContext start  
  17. 嚴重: Error filterStart  
  18. 2013-8-9 15:38:59 org.apache.catalina.core.StandardContext start  
  19. 嚴重: Context [/solr] startup failed due to previous errors  

很顯然,提示我們找不到日誌相關的jar包,還提示我們去看solr的相關wiki鏈接:http://wiki.apache.org/solr/SolrLogging

到該Wiki頁面中我們會找到如下幾段文字說明:

What changed

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

  1. Copy the jars from solr/example/lib/ext into your container's main lib directory. These jars will set up SLF4J and log4j.

    1. 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.

  2. 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.

  3. 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成功啓動,我們通過瀏覽器進行訪問,你將會看到如下畫面:



在Solr中使用中文分詞


使用全文檢索,中文分詞是離不開的,這裏我採用的是mmseg4j分詞器。mmseg4j分詞器內置了對solr的支持,最新版本可支持4.X版本的sorl,使用起來很是方便。

GoogleCode地址:http://code.google.com/p/mmseg4j/

使用mmseg4j中文分詞器,首先需要在schema.xml文件中配置一個fieldType節點(其它的中文分詞器也類似),如下所示:

[html] view plaincopy
  1. <!-- 中文分詞 -->  
  2. <fieldType name="text_zh" class="solr.TextField" positionIncrementGap="100">  
  3.     <analyzer>   
  4.         <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" />  
  5.     </analyzer>  
  6. </fieldType>  

然後就可以在field節點中引用該filedType了,定義示例filed節點如下:

[html] view plaincopy
  1. <field name="title" type="text_zh" indexed="true" stored="true" multiValued="true"/>  
  2. <field name="content" type="text_zh" indexed="true" stored="false" multiValued="true"/>  

然後在瀏覽器裏試一下效果:



總結


下面總結一下Solr4.4.0的安裝與配置步驟:

  1. 準備Java和Tomcat;
  2. 下載並解壓solr-4.4.4.zip得到solr-4.4.0目錄;
  3. 拷貝solr-4.4.0/example/webapps/solr.war包到%Tomcat_Home%/webapps目錄,啓動Tomcat使其解壓,刪除solr.war包;
  4. 打開%Tomcat_Home%/webapps/solr/WEB-INF/web.xml文件,配置SOLR_HOME,按照SOLR_HOME的配置拷貝solr-4.4.0/example/solr目錄(該目錄即作爲solr_home);
  5. 拷貝solr-4.4.0/example/lib/ext下所有的jar包到項目的classpath下,同時拷貝log4j.properties配置文件到classpath;
  6. 配置中文分詞器。  


======================================================================================================


windows7下安裝solr4.4.0<二>


下載:http://www.apache.org/dyn/closer.cgi/lucene/solr/4.4.0

下載完成後直接解壓至F:\solr

4. slor安裝至tomcat

a. f:\solr\examples下的slor文件夾複製到f:\tomcat 下面,作爲solrhome目錄

b. f:\solr\examples\webapps下的solr.war複製到tomcatwebapps目錄下,war包在tomcat啓動後會自動解壓出來

c. 配置solrdata目錄(在solr目錄下新建文件夾data),打開f:/tomcat/solr/collection1/conf/solrconfig.xml,修改datadir

<!-- Data Directory  
       Used to specify an alternate directory to hold all index data  
       other than the default ./data under the Solr home.  If  
       replication is in use, this should match the replication  
       configuration.  
    -->  
  <dataDir>${solr.data.dir:F:\tomcat\solr\data}</dataDir>  

d. solr.war註冊給tomcat,在f:/tomcat/conf/Catalina/localhost/下創建solr.xml:

<?xml version="1.0" encoding="UTF-8"?>  
<Context docBase="f:/tomcat/webapps/solr" debug="0" crossContext="true" >  
   <Environment name="solr/home" type="java.lang.String" value="f:/tomcat/solr" override="true" />  
</Context>  

e. 防止亂碼的在tomcat/conf/server.xml中加入;

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/> 

f. F:\solr\example\lib\ext下的5jar包和F:\solr\example\resources下的log4j放入tomcatlib目錄下

g. 重新啓動tomcat,瀏覽器中輸入:http://localhost:8080/solr



 




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章