日期:2014-05-16  浏览次数:20399 次

Solr将数据库中table中的数据导入到索引
Solr将数据库中table中的数据导入到索引

版权信息: 可以任意转载, 转载时请务必以超链接形式标明文章原文出处, 即下面的声明.
原文出处:http://tbwuming.iteye.com/blog/1152333

1、首先在MySQL中创建表,并写入数据
-- ----------------------------
-- Table structure for course
-- ----------------------------
CREATE TABLE `course` (
  `id` int(11) NOT NULL,
  `user` varchar(20) NOT NULL,
  `title` varchar(50) NOT NULL,
  `content` varchar(200) NOT NULL,
  `time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `course` VALUES ('1', 'tiehong', '搜索', '这一小节讲搜索引擎,可以通过搜索引擎查找一些数据', '2011-08-18 10:41:35');
INSERT INTO `course` VALUES ('2', 'chenyun', '存储', '这一小节讲分布式存储,可以存储很多数据', '2011-08-18 10:42:19');
INSERT INTO `course` VALUES ('3', 'lengda', '数据导入', '这一小节讲数据导入,可以从mysql数据库中导入数据', '2011-08-18 10:42:59');


2、修改apache-solr-3.3.0\example\solr\conf\schema.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<schema name="example" version="1.4">
  <types>    
     <fieldType name="tint" class="solr.TrieIntField" precisionStep="8" omitNorms="true" positionIncrementGap="0"/>

     <fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/>

     <fieldType name="text" class="solr.TextField" positionIncrementGap="100">
      <analyzer type="index">
        <tokenizer class="solr.StandardTokenizerFactory"/>
        <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
        <filter class="solr.LowerCaseFilterFactory"/>
      </analyzer>
      <analyzer type="query">
        <tokenizer class="solr.StandardTokenizerFactory"/>
        <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
        <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
        <filter class="solr.LowerCaseFilterFactory"/>
      </analyzer>
    </fieldType>

     <fieldType name="date" class="solr.TrieDateField" omitNorms="true" precisionStep="0" positionIncrementGap="0"/>  
</types>

<fields>
   <field name="id" type="tint" indexed="true" stored="true" required="true" />
   <field name="user" type="string" indexed="true" stored="true"/>
   <field name="title" type="text" indexed="true" stored="true"/> 
   <field name="content" type="text" indexed="true" stored="true" /> 
   <field name="time" type="date" indexed="true" stored="true" default="NOW"/>  
</fields>

<uniqueKey>id</uniqueKey>
<defaultSearchField>title</defaultSearchField> 
<solrQueryParser defaultOperator="AND"/>  

</schema>



3、编辑apache-solr-3.3.0\example\solr\conf\solrconfig.xml文件,添加内容如下:
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> 
          <lst name="defaults"> 
               <str name="config">data-config.xml</str> 
          </lst> 
</requestHandler>



4、创建data-config.xml文件,放在apache-solr-3.3.0\example\solr\conf\目录下,文件内容如下:
<dataConfig> 
  <dataSource type="JdbcDataSource" 
              driver="com.mysql.jdbc.Driver" 
              url="jdbc:mysql://localhost/dbname" 
              user="user-name" 
              password="password"/> 
  <document> 
    <entity name="id" 
            query="select * from course"> 
    </entity> 
  </document> 
</dataConfig>  


5、将MySQL的JDBC驱动放在apache-solr-3.3.0\example\lib目录下