现在的位置: 首页 > 综合 > 正文

solr运行配置与数据库数据导入到solr

2017年05月25日 ⁄ 综合 ⁄ 共 3765字 ⁄ 字号 评论关闭

一,运行

1,solr运行容器,tomcat

         2,拷贝apache-solr-3.6.0.war到tomcat的webapps目录下,并改名为solr.war

        3,tomcat的conf目录下建立结构为conf/Catalina/localhost的两个文件夹。建立结构为solr-tomcat/solr的两个文件夹(solr的HOME目录),如建在D盘根目录,d:/solr-tomcat/solr,solr-tomcat文件夹名字可任意命名,将apache-solr-3.6.0\example\solr下的所有文件及文件夹拷贝到这下面

        4,在localhost文件夹下建立solr.xml,并保存如下内容:

<Context docBase="D:\tomcat-6.0\webapps\solr.war" debug="0" crossContext="true" >   
      <Environment name="solr/home" type="java.lang.String" value="D:/solr-tomcat/solr" override="true" />   
</Context>

         5,此时可以运行tomcat,地址栏输入:http://localhost:8080/solr/admin进行验证

         6,开始为导入数据库数据添加配置。将jdbc驱动jar和apache-solr-3.6.0\dist\apache-solr-dataimporthandler-3.6.0.jar 两个jar拷贝到tomcat的webapps/solr/WEB-INF/lib下。将apache-solr-3.6.0\example\example-DIH\solr下的所有文件及文件夹拷贝(并覆盖)到solr的HOME目录,如:d:/solr-tomcat/solr

         7,更改solr Home目录下的conf/solrconfig.xml,添加如下内容:

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">D:\solr-tomcat\solr\db\conf\db-data-config.xml</str> <!--  根据自己电脑里的db-data-config.xml的实际路径来写 -- >
</lst>
</requestHandler>

      8, 将solr Home目录下面的solrconfig.xml和schema.xml拷贝到db文件夹下面的conf中,注意:导入的字段要先在schema.xml中定义

                   定义如:<field name="firstname" type="string" stored="true" indexed="true"/>

      9,修改db\conf\db-data-config.xml,可参考如下:

                                  <dataConfig> 
 <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/langsin1" user="root" password="root"/> 
  <document name="userss"> 
      <entity name="users" pk="id" query="select * from users"> 
      <field column="id" name="id" /> 
       <field column="firstname" name="firstname" /> 
<field column="lastname" name="lastname" /> 
<field column="age" name="age" /> 
    </entity> 
  </document> 
</dataConfig>

               10,启动TOMCAT,输入地址进行导入,导入分为很多模式:我选用的全部倒入模式

                                 

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

          11,如果有中文,修改tomcat的server.xml文件

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

           12,添加中文分词,如:mmseg4j, 在$SOLR_HOME下建立lib和dic两个目录,讲mmseg4j-all-1.8.4.jar拷贝到lib目录,将data里的.dic文件拷贝到dic目录


         13,

修改Schema.xml

添加fieldType

Xml代码 复制代码 收藏代码
  1.  <types>  
  2.       <fieldType name="textComplex" class="solr.TextField" positionIncrementGap="100" >    
      
  3.            <analyzer>    
      
  4.               <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="/opt/solr/example/solr/dic"/>    
      
  5.               <filter class="solr.LowerCaseFilterFactory"/>    
      
  6.           </analyzer>    
      
  7.       </fieldType>    
      
  8.     
  9.     <fieldType name="textMaxWord" class="solr.TextField" positionIncrementGap="100" >    
      
  10.        <analyzer>    
      
  11.            <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="max-word" dicPath="/opt/solr/example/solr/dic"/>    
      
  12.            <filter class="solr.LowerCaseFilterFactory"/>    
      
  13.        </analyzer>    
      
  14.     </fieldType>    
      
  15.          
  16.     <fieldType name="textSimple" class="solr.TextField" positionIncrementGap="100" >    
      
  17.       <analyzer>    
      
  18.           <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" dicPath="/opt/solr/example/solr/dic"/>    
      
  19.           <filter class="solr.LowerCaseFilterFactory"/>    
      
  20.       </analyzer>    
      
  21.     </fieldType>  
      
  22.   
  23. ..   
  24. </types>  

 

Xml代码 复制代码 收藏代码
  1. <field name="simple" type="textSimple" indexed="true" stored="true" multiValued="true"/>    
      
  2. <field name="complex" type="textComplex" indexed="true" stored="true" multiValued="true"/>    
      
  3. <field name="maxword" type="textMaxWord" indexed="true" stored="true" multiValued="true"/>   

 

Xml代码 复制代码 收藏代码
  1. <copyField source="simple" dest="text"/>  
      
  2. <copyField source="complex" dest="text"/> 
      
         14,重启tomcat,

进入 http://yourhost:8080/solr-example/admin/analysis.jsp,测试中文分词

抱歉!评论已关闭.