Solr学习三与mysql集成建立索引

时间:2021-05-11 16:45:21

目前百度上面关于solr对MySQL的集成一般都是4.0左右。

但是最新的solr的版本已经到了6.x,很多配置都和一起不一样了。接着上篇文章讲讲solr6.x如何进行MYSQL数据库的配置。

首先在solrconfig.xml的<requestHandler name="/select" class="solr.SearchHandler">之上添加

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">  
  <lst name="defaults">
  <str name="config">data-config.xml</str>
  </lst>
</requestHandler>
然后在conf下新建data-config.xml文件。里面内容如下:

<?xml version="1.0" encoding="UTF-8"?>  
<dataConfig>
<dataSource name="source1" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/zhsoftbank" user="root" password="root" batchSize="-1" />
<document>
<entity name="news" pk="id" dataSource="source1"
query="select * from news"
deltaImportQuery="select * from news where id='${dih.delta.id}'"
deltaQuery="select id from news where updateTime> '${dataimporter.last_index_time}'">

<field column="id" name="id"/>
<field column="title" name="title"/>
<field column="synopsis" name="synopsis"/>
<field column="updateTime" name="updateTime"/>
</entity>
</document>
</dataConfig>

dataSource是数据库数据源。Entity就是一张表对应的实体,pk是主键,query是查询语句。Field对应一个字段,column是数据库里的column名,后面的name属性对应着Solr的Filed的名字。其中solrdata是数据库名,goods是表名。
其中deltaQuery是增量索引,原理是从数据库中根据deltaQuery指定的SQL语句查询出所有需要增量导入的数据的ID号。然后根据deltaImportQuery指定的SQL语句返回所有这些ID的数据,即为这次增量导入所要处理的数据。核心思想是:通过内置变量“${dih.delta.id}”和 “${dataimporter.last_index_time}”来记录本次要索引的id和最近一次索引的时间。

  然后把mysql所需的jar包和solr-6.2.0\dist下的solr-dataimporthandler-6.2.0.jar和solr-dataimporthandler-extras-6.2.0.jar都复制到项目WEB-INF\lib下。

启动Tomcat,输入http://localhost:8080/solr/index.html按如下选择,Solr学习三与mysql集成建立索引

点击查询Solr学习三与mysql集成建立索引

已经成功导入数据

数据库数据脚本