solr5.3.1从mysql导入索引

来源:互联网 发布:淘宝手机测量尺码在哪 编辑:程序博客网 时间:2024/06/08 10:50

1.前提

本文假设你已经搭建好solr服务器并配置core核心(本文以solr5.3.1的db例子作为实例

2.copy jar包和创建表

将mysql放到solr所在的服务器所在项目下的lib下;例如我的是D:\Tomcat7\webapps\solr\WEB-INF\lib

表结构:

CREATE TABLE `t_blog` (  `id` int(200) NOT NULL AUTO_INCREMENT,  `title` varchar(200) DEFAULT NULL,  `content` text,  `keyWord` varchar(200) DEFAULT NULL,  `category_id` int(200) DEFAULT NULL,  `user_id` int(200) DEFAULT NULL,  `insertTime` time DEFAULT NULL,  `updateTime` time DEFAULT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;

3.配置solrconfig.xml

确保一下配置文件被打开(5.3.1默认打开)

 <requestHandler name="/dataimport" class="solr.DataImportHandler">    <lst name="defaults">      <str name="config">db-data-config.xml</str>    </lst>  </requestHandler>

4.配置db-data-config.xml

增加数据源和docment

 <dataSource type="JdbcDataSource"                          driver="com.mysql.jdbc.Driver"                          url="jdbc:mysql://localhost:3306/blog"                          user="root"                          password="root"/>  <document name="t_blog">                 <entity name="t_blog" pk="t_id"                         query="select id,title,content,keyWord from t_blog"                         deltaImportQuery="select id,title,content,keyWord from t_blog where ID='${dataimporter.delta.id}'"                          deltaQuery="select id  from t_blog where updateTime > '${dataimporter.last_index_time}'"                           deletedPkQuery="select id  from t_blog where id=0">                        <field column="id" name="t_id" />                        <field column="title" name="t_title" />                        <field column="content" name="t_tcontent"/>        <field column="keyWord" name="t_keyWord"/>                   </entity>  </document> 
solr官方例子中的db core带有id,content等属性,故我将个人设置的field name前面加了‘t_’标示

备注:

query 用于首次导入;
deltaQuery用于增量导入;
deltaImportQuery用于单条数据导入;
deletedPkQuery用于删除所有表索引;

5.配置schema.xml

   <!-- mysql -->   <field name="t_id" type="int" indexed="true" stored="true" required="true" />    <field name="t_title" type="string" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true"/>    <field name="t_content" type="text_general" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true"/>    <field name="t_keyWord" type="string" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true"/>    <!-- mysql -->

5.测试与结果

启动solr所在的tomcat服务器,访问http://localhost:8080/solr/

选择db core -> dataimport ->Command -> full import -> Entity -> t_blog ->Execute



6.后记

更多精彩内容,欢迎访问我的博客:caicongyang

http://blog.csdn.net/caicongyang





1 0
原创粉丝点击