Solr4.4.0的安装与配置

最近准备用Solr搭建项目,所以对其作了一些了解,我采用的是Solr4.4版本;这个版本的Solr相对于以前的版本改变很大,这里记一下自己安装与配置的过程。

网上很多关于Solr的教程都很老了,很多教程居然还是基于Solr1.3或者1.4版本在讲解,有些资料的内容看了再对比当前版本会不知所云。

尽量多看官方提供的wiki文档:http://wiki.apache.org/solr/

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. 配置中文分词器。
原文地址:https://www.cnblogs.com/LiaoHao/p/3585536.html