Solr入门学习

从头开始学起,遇到的问题也一一记录在这里。

一.环境搭建

之前下载了最新版solr 6.0,但6.0对Java的版本要求太高,我安装的JDK1.7,启动solr就会报错。
  • 在桌面上建立一个文件夹solr。 拷贝一个tomcat到solr文件夹下,并将其改名为server。

  • 将solr-4.10.4\dist下的solr-4.10.4.war拷贝tomcat容器的webapps下,这是solr的主目录,将其改名为solr.war。

  • 从solr-4.10.4\example下拷贝solr文件夹到桌面的solr下,并将其改名为home。然后将tomcat启动。

  • 在server\conf的server.xml的标签中增加,path为空表示从根目录访问。
<Context path="" docBase="solr" reloadable="false" crossContext="true">
          <Environment name="solr/home" type="java.lang.String" value="C:\Users\happy\Desktop\solr\home" override="true"/>
        </Context>
  • Solr启用验证,在server/conf/tomcat-user.xml的中增加(非必要)
<user password="admin" roles="manager-script,admin" username="admin"/>
<role rolename="solr"/>
<user username="admin" password="admin" roles="solr"/>
  • 在 server/webapps下的ROOT和solr下找到web.xml,在其中增加(非必要)
 <security-constraint>
      <web-resource-collection>
        <web-resource-name>Solr Lockdown</web-resource-name>
        <url-pattern>/</url-pattern>
      </web-resource-collection>
      <auth-constraint>
        <role-name>solr</role-name>
        <role-name>admin</role-name>
      </auth-constraint>
    </security-constraint>
    <login-config>
      <auth-method>BASIC</auth-method>
      <realm-name>Solr</realm-name>
    </login-config>

这样以后每次访问localhost:8080时,都会弹框要求输入用户名与密码。

我们一般不适用solr的后台,而是使用solr的客户端,比如Java对应的客户端就是solrj,其他PHP,Ruby,python等都有相应的客户端。
如果solr开启了用户验证,那么solr客户端还需要做一些配置:

创建communityserver_override.config文件,放到webapps下,在其中写

<?xml version="1.0" encoding="utf-8"?>
<Overrides>
    <!--修改搜索服务器的url-->
    <Override xpath="/CommunityServer/Search/Solr" mode="change" name="host" value="http://localhost:8080"/>
</Overrides>

注意value值需要与tomcat的配置一样。path被修改的话这里也要相应更改。

错误处理:

  • 启动后访问会报404错误,在tomcat\logs下查看日志,报错:

java.lang.NoClassDefFoundError: Failed to initialize Apache Solr: Could not find necessary SLF4j logging jars. If using Jetty, the SLF4j logging jars need to go in the jetty lib/ext directory. For other containers, the corresponding directory should be used. For more information, see: http://wiki.apache.org/solr/SolrLogging

  • 提示缺少jar包。将solr-5.2.0\server\lib\ext中的jar包放到tomcat的lib中。 启动tomcat,
    尝试访问solr,继续报错。
{msg=SolrCore 'collection1' is not available due to init failure: Could not load conf for core collection1: Error loading solr config from solr/collection1\conf\solrconfig.xml,trace=org.apache.solr.common.SolrException: ……
  • 在solr/WEB-INF的web.xml中,去掉这一段的注释,并且将目录改成自己sorl所在的位置。

<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>C:\Users\happy\Desktop\solr\home</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>

二.Solr的中文支持

solr的中文支持和lucence的中文支持是一样的。如果不增加中文支持,Solr就不能用来检索中文,所以需要安装分词器。

  • 去maven仓库中下载MMSEG4J分词器。下载到mmseg4j-core.jar,mmseg4j-solr.jar,mmseg4j-analysis.jar这几个jar包。
    将3个jar包拷贝到solr\WEB-INF\lib下。
  • 接下来配置一下分词器,去mmseg4j的github上复制以下代码,将dicPath的部分修改为相应库文件路径
<fieldtype name="textComplex" class="solr.TextField" positionIncrementGap="100">
    <analyzer>
        <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="../dic"/>
    </analyzer>
</fieldtype>
<fieldtype name="textMaxWord" class="solr.TextField" positionIncrementGap="100">
    <analyzer>
        <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="max-word" />
    </analyzer>
</fieldtype>
<fieldtype name="textSimple" class="solr.TextField" positionIncrementGap="100">
    <analyzer>
        <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" dicPath="../dic" />
    </analyzer>
</fieldtype>
  • 在home\collection1\conf下找到schema.xml,并将代码拷贝到fieldType上面。三个fieldtype代表了3类分词器,复制textComplex,用其修改xml中field中部分内容的type属性。修改后为:
<field name="title" type="textComplex" indexed="true" stored="true" multiValued="true"/>
   <field name="subject" type="textComplex" indexed="true" stored="true"/>
   <field name="description" type="textComplex" indexed="true" stored="true"/>
   <field name="comments" type="textComplex" indexed="true" stored="true"/>
   <field name="author" type="textComplex" indexed="true" stored="true"/>
   <field name="keywords" type="textComplex" indexed="true" stored="true"/>

在home下创建dic文件夹。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值