从数据库中导入数据到solr2

要建立自己的全文检索,一般都需要从数据库导入数据,在原来配置的基础上,增加导入的功能,这里以mysql为例子:

solr的工作目录中选择一个core,我这里选择core1。进入配置文件夹:solr_tomcat\solr\core1\conf 。在solrconfig.xml中添加如下代码:


[html] view plain copy
  1. <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">    
  2.    <lst name="defaults">    
  3.     <str name="config">data-config.xml</str>    
  4.    </lst>    
  5.  </requestHandler>  


在同一目录下(配置文件夹)下新建data-config.xml,添加以下代码:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <dataConfig>  
  3.  <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver"    
  4.    url="jdbc:mysql://localhost:3306/li"    
  5.    user="root"    
  6.    password="465864"/>  
  7.    <document name="lhx">  
  8.       <entity name="student" pk="sid" query="select sid,sname,sage,saddress,sdescript from student">    
  9.           <field column="sid" name="id" />    
  10.           <field column="sname" name="name" />    
  11.           <field column="sage" name="age" />  
  12.           <field column="saddress" name="address" />  
  13.           <field column="sdescript" name="descript" />   
  14.       </entity>  
  15.  </document>   
  16. </dataConfig>  

修改相应的urluserpassword<document name="lhx">这个随便取名。

<entity name="student" pk="sid" query="select sid,sname,sage,saddress,sdescript from student">  

name指表名,pk是主键名,query是查询SQL语句。

[html] view plain copy
  1. <field column="sid" name="id" />  


column是列名,对应数据库中字段的名称,name就是solr这边对应的名称。接下来就是配置name了,这要到schema.xml里面配置,现在打开这个文件,原来有的,可以保留,没有的就添加,最后的内容为:

[html] view plain copy
  1. <?xml version="1.0" ?>  
  2. <!--  
  3.  Licensed to the Apache Software Foundation (ASF) under one or more  
  4.  contributor license agreements.  See the NOTICE file distributed with  
  5.  this work for additional information regarding copyright ownership.  
  6.  The ASF licenses this file to You under the Apache License, Version 2.0  
  7.  (the "License"); you may not use this file except in compliance with  
  8.  the License.  You may obtain a copy of the License at  
  9.   
  10.      http://www.apache.org/licenses/LICENSE-2.0  
  11.   
  12.  Unless required by applicable law or agreed to in writing, software  
  13.  distributed under the License is distributed on an "AS IS" BASIS,  
  14.  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  15.  See the License for the specific language governing permissions and  
  16.  limitations under the License.  
  17. -->  
  18.   
  19. <schema name="example core one" version="1.1">  
  20.   
  21.    <fieldtype name="string"  class="solr.StrField" sortMissingLast="true" omitNorms="true"/>  
  22.    <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>  
  23.   
  24.   
  25.   
  26.   
  27.     
  28.    <fieldType name="float" class="solr.TrieFloatField" precisionStep="0" positionIncrementGap="0"/>  
  29.    <fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/>   
  30.    <fieldType name="date" class="solr.TrieDateField"  precisionStep="0" positionIncrementGap="0"/>    
  31.    <fieldType name="text_ik" class="solr.TextField">     
  32.      <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>     
  33.    </fieldType>  
  34.   
  35.    
  36.      
  37.   <field name="id" type="long" indexed="true"  stored="true" multiValued="false" required="true"/>    
  38.   <field name="name" type="string" indexed="true"  stored="true"  multiValued="false" />   
  39.   <field name="age" type="int" indexed="true"  stored="true" multiValued="false" />   
  40.   <field name="address" type="string" indexed="true"  stored="true" multiValued="false" />  
  41.   <field name="descript" type="text_ik" indexed="true" stored="true" multiValued="false" />     
  42.      
  43.   
  44.   
  45.   <!-- general -->  
  46.   
  47.   <field name="type"      type="string"    indexed="true"  stored="true"  multiValued="false" />   
  48.   <field name="core1"     type="string"    indexed="true"  stored="true"  multiValued="false" />  
  49.   <field name="_version_" type="long"      indexed="true"  stored="true"/>  
  50.   
  51.  <!-- field to use to determine and enforce document uniqueness. -->  
  52.  <uniqueKey>id</uniqueKey>  
  53.   
  54.  <!-- field for the QueryParser to use when an explicit fieldname is absent -->  
  55.  <defaultSearchField>name</defaultSearchField>  
  56.   
  57.  <!-- SolrQueryParser configuration: defaultOperator="AND|OR" -->  
  58.  <solrQueryParser defaultOperator="OR"/>  
  59. </schema>  

text_ik是一个中文分词器ik-analyzer,专门处理中文分词。

申明fieldfield的名字应该和sql的查询结果集列名一致,如果不一致,需要在data-config.xmlentity标签中用field指明列和field的对应关系。

如下field是必须的,用于标记版本信息,由solr内部自己维护。

<field name="_version_" type="long"      indexed="true"  stored="true"/>

这个原来就有,不用修改。

 

该配置的东西都配置好了,现在就是往MySQL数据库里面写入内容,整个的SQL语句提供给大家:

  1. SET FOREIGN_KEY_CHECKS=0;  
  2.   
  3. -- ----------------------------  
  4. -- Table structure for student  
  5. -- ----------------------------  
  6. DROP TABLE IF EXISTS `student`;  
  7. CREATE TABLE `student` (  
  8.   `sid` bigint(20) NOT NULL,  
  9.   `sname` varchar(255) DEFAULT NULL,  
  10.   `sage` int(11) DEFAULT NULL,  
  11.   `saddress` varchar(255) DEFAULT NULL,  
  12.   `sdescript` text,  
  13.   PRIMARY KEY (`sid`)  
  14. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
  15.   
  16. -- ----------------------------  
  17. -- Records of student  
  18. -- ----------------------------  
  19. INSERT INTO `student` VALUES ('1''李四''23''中华路1号''好学生');  
  20. INSERT INTO `student` VALUES ('2''张三''34''华信路3号''坏学生,经常做坏事!');  

还要到solr的开发包里面找相应的jar包,位置:solr-4.10.2\dist ,有两个:

solr-dataimporthandler-extras-4.10.2

solr-dataimporthandler-4.10.2

还有一个jtds-1.2.4,自己到网上下载。

最后不忘了MySQL的连接包:mysql-connector-java-5.1.30

把这些都放到apache-tomcat-6.0.43\webapps\solr\WEB-INF\lib里面去。

启动tomcat,不报错就可以了!


打开solr,选择core1,下面的条目选择“Dataimport”,右边选择full-import,接着就是Execute执行,右边会显示“Indexing……”

不要一直等待,你一直等待,它都是这个样子的,要执行第4步:Refresh Status 。最后会出现:


到此大功告成!接着做以下查询,用到了IK Analyzer分词。


选择“Query,接着在右边的q里面输入“要查询的字段”:“值”,查询的字段对应在data-config.xml对应的name属性名称,而不是数据表的字段名称。“wt”代表响应的数据,默认是json,不用修改,最后就是点击“Execute Query”按钮了。结果会马上呈现在右边的空白处。

 

后记:

一开始schema.xml里面的int类型我写成了以下的代码,启动solr后报出了错误。

[html] view plain copy
  1. <fieldType name="int"   class="solr.TrieIntegerField" precisionStep="0" positionIncrementGap="0"/>   

Tomcat里面报出的错误提示就更容易理解了:

怎么写int类型才准确呢?我到collection1里面查考官网的带的schema.xml,里面就有对int的定义:

[html] view plain copy
  1. <fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/>  

修改后这个问题就解决了。

 

再重启solrtomcat里面报出如下错误:

可见是缺少jar包了,添加solr-dataimporthandler-extras-4.10.2solr-dataimporthandler-4.10.2jtds-1.2.4。解决问题!

 

接着就是导入数据分析的时候一直卡在“Indexing……”字样,点击刷新也没反应,在tomcat里面可以看到如下错误提示:

缺少数据库驱动jar包,导入就没问题了!

 

搜索的时候,字段名写得不正确,是有红色波浪线提示的:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值