solr5.3.1从mysql导入索引

本文详细介绍如何将Solr与MySQL进行整合,包括配置步骤、关键文件修改等内容,并提供了从零开始实现全文检索系统的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值