温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Solr环境如何配置

发布时间:2021-12-07 14:14:30 来源:亿速云 阅读:174 作者:小新 栏目:云计算

这篇文章主要为大家展示了“Solr环境如何配置”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Solr环境如何配置”这篇文章吧。

一、配置solr环境

1、  下载solr  http://www.apache.org/dyn/closer.cgi/lucene/solr/

此处用的是4.6 解压到D盘D:/solr/apache-solr-4.6.0

2、修改tomcat conf/server.xml

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

添加编码的配置 URIEncoding="UTF-8" (如不添加,中文检索时因为乱码搜索不到)

3、  配置solr

Tomcat  conf/Catalina/localhost下添加solr.xml

内容:

<Context docBase="D:/solr/apache-solr-4.6.0/dist/apache-solr-4.6.0.war" debug="0" crossContext="true" >

<Environment name="solr/home" type="java.lang.String" value="D:/solr/apache-solr-4.6.0/example/solr" override="true" />

</Context>

4、 启动tomcat  输入http://localhost:8080/solr/  出现欢迎界面表示成功

二、中文分词配置

1、 下载分词器: http://code.google.com/p/mmseg4j/

2、下载词库:http://code.google.com/p/mmseg4j/downloads/detail?name=data.zip&can=2&q

3、将解压后的mmseg4j-1.8.5目录下的mmseg4j-all-1.8.5.jar拷贝到Tomcat的webapps/solr/WEB-INF/lib目录下。

4、添加词库:在D:/solr/apache-solr-4.6.0/example/solr目录下新建dic文件夹,将解压后的data目录下的words.dic拷贝到D:/solr/apache-solr-4.6.0/example/solr/dic目录下。

5、  更改schema.xml(D:/solr/apache-solr-4.6.0/example/solr/conf/)文件,使分词器起到作用。

在schema.xml的<types>、<fields>和部分新增如下配置:

注:dicPath=" "是你的词库路径。

    <!--mmseg4j field types-->

       <fieldType name="textComplex" class="solr.TextField" positionIncrementGap="100" > 

                  <analyzer> 

                     <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="D:/solr/apache-solr-4.6.0/example/solr/dic"/> 

                     <filter class="solr.LowerCaseFilterFactory"/> 

           </analyzer> 

       </fieldType> 

     <fieldType name="textMaxWord" class="solr.TextField" positionIncrementGap="100" > 

        <analyzer> 

            <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="max-word" dicPath="D:/solr/apache-solr-4.6.0/example/solr/dic"/> 

            <filter class="solr.LowerCaseFilterFactory"/> 

        </analyzer> 

     </fieldType> 

     <fieldType name="textSimple" class="solr.TextField" positionIncrementGap="100" > 

       <analyzer> 

           <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" dicPath="D:/solr/apache-solr-4.6.0/example/solr/dic"/> 

           <filter class="solr.LowerCaseFilterFactory"/> 

       </analyzer> 

     </fieldType>

   <!-- mmseg4j field -->

   <field name="simple" type="textSimple" indexed="true" stored="true" multiValued="true"/> 

   <field name="complex" type="textComplex" indexed="true" stored="true" multiValued="true"/> 

   <field name="text_mmseg4j" type="textMaxWord" indexed="true" stored="true" multiValued="true"/>

  <!-- mmseg4j copyField -->

   <copyField source="simple" dest="text_mmseg4j"/>

   <copyField source="complex" dest="text_mmseg4j"/>

重启你的tomcat。

访问:http://localhost:8080/solr/admin/analysis.jsp可以看 mmseg4j 的分词效果。在 Field 的下拉菜单选择 name,然后在应用输入 complex。分词的结果,如下图:

好了,可以运行起来了,那就添加个文档试下,在 解压后的D:/solr/apache-solr-4.6.0/example/exampledocs目录下创建 mmseg4j-solr-demo-doc.xml 文档,内容如下:

  <add> 

      <doc> 

          <field name="id">1</field> 

          <field name="text">高效、灵活的缓存功能,垂直搜索功能。</field> 

      </doc> 

      <doc> 

          <field name="id">2</field> 

          <field name="text"> Solr是一个高性能,采用Java5开发,基于Lucene的全文搜索服务器。</field> 

      </doc> 

      <doc> 

          <field name="id">3</field> 

          <field name="text"> 独立的企业级搜索应用服务器</field> 

      </doc> 

  </add>

注意:使用的字段name必须在solr的配置文件schem.xml增加

       下(id、title)一样

然后在 cmd 下运行 post.jar,如下:

D:\solr\apache-solr-4.6.0\example\exampledocs>java -Durl=http://localhost:8080/

solr/update -Dcommit=yes -jar post.jar mmseg4j-solr-demo-doc.xml 

注意:mmseg4j-solr-demo-doc.xml 要是 UTF-8 格式,不然提交后会乱码。还有在查询中文时需要把tomcat设置成URIEncoding="UTF-8";

查看是否有数据,访问:http://localhost:8080/solr/admin/ 在Query String: 中输入“高性能”

显示如下图所示:

三、Solr将数据库做成索引数据源(以mysql为例)

1、  在solrconfig.xml中配置导入数据功能(D:\solr\apache-solr-4.6.0\example\solr\conf下)

     <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> 

     <lst name="defaults"> 

       <str name="config">D:\solr\apache-solr-4.6.0\example\solr\conf\db\db-data-config.xml</str> 

     </lst> 

  </requestHandler>

2、  添加一个数据源db-data-config.xml (D:\solr\apache-solr-4.6.0\example\solr\conf\db下)

    <dataConfig>

         <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/solr" user="root" password="root"/>

             <document name="messages">

               <entity name="user" transformer="ClobTransformer" query="select * from t_user">

             <field column="id" name="id" />

                      <field column="name" name="name"/>

                      <field column="title" name="title"/>

         </entity>

     </document>

 </dataConfig>

CREATE TABLE t_user  

(  

   id         VARCHAR(255) PRIMARY KEY,  

   name       VARCHAR(10) NOT NULL,  

   title        VARCHAR(10)

);

3、放入mysql驱动包  (Tomcat的webapps/solr/WEB-INF/lib目录下)

4、创建索引

   http://localhost:8080/solr/dataimport?command=full-import

5、  查看是否成功,访问:http://localhost:8080/solr/admin/ 在Query String: 中输入“sunshan”、“孙闪”

如下图:

注:保证与数据库连接正确

四、Solr多核(MultiCore)配置

1、  拷贝multicore(D:\solr\apache-solr-4.6.0\example下)下的core0、core1、exampledocs到solr(D:\solr\apache-solr-4.6.0\example\solr下)目录下

2、  更改solr.xml(D:\solr\apache-solr-4.6.0\example\solr下)配置

添加:

<core name="core0" instanceDir="core0" dataDir="D:/solr/apache-solr-4.6.0/example/solr/core0/data"/>

<core name="core1" instanceDir="core1" dataDir="D:/solr/apache-solr-4.6.0/example/solr/core1/data"/>

配置后:

   <cores adminPath="/admin/cores">

    <core name="collection1" instanceDir="." />

  <core name="core0" instanceDir="core0" dataDir="D:/solr/apache-solr-4.6.0/example/solr/core0/data"/>

    <core name="core1" instanceDir="core1" dataDir="D:/solr/apache-solr-4.6.0/example/solr/core1/data"/>

  </cores>

3、  启动tomcat 访问:

http://localhost:8080/solr/

五、多核数据库索引:

同上配置

注:出现错误Error loading class 'org.apache.solr.handler.dataimport.DataImportHandler'

需配置(solrconfig.xml里)

  <!-- start -->

  <lib dir="D:/solr/apache-solr-4.6.0/contrib/extraction/lib" />

  <lib dir="D:/solr/apache-solr-4.6.0/dist/" regex="apache-solr-cell-\d.*\.jar" />

  <lib dir="D:/solr/apache-solr-4.6.0/dist/" regex="apache-solr-clustering-\d.*\.jar" />

  <lib dir="D:/solr/apache-solr-4.6.0/dist/" regex="apache-solr-dataimporthandler-\d.*\.jar" />

  <lib dir="D:/solr/apache-solr-3.3.0/contrib/clustering/lib/" />

  <lib dir="/total/crap/dir/ignored" />

  <!-- end -->

dir注意路径

重启tomcat

创建索引:http://localhost:8080/solr/core1/dataimport?command=full-import

http://localhost:8080/solr/dataimport?command=full-import 这 url 告诉 solr 做全量索引,做索引中会删除所有数据。当然也可以用 clean=false 参数来告诉它不删除,但也会删除相同id的(在 scheam.xml 的uniqueKey 声明的)。http://localhost:8080/solr/dataimport?command=full-import&clean=false

批量导入command=delta-import&commit=y

重新装载配置文件command=reload-config

终止导入command=abort

以上是“Solr环境如何配置”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI