1、将连接MySQL以及solr导入数据所需要的资源包导入到lib里:
从solr的源码包的dist目录下(例如,C:\APP\solr-8.6.0\dist)取出两个jar包,solr-dataimporthandler-8.6.0.jar、solr-dataimporthandler-extras-8.6.0.jar;
在https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.21里下载mysql-connector-java-8.0.21.jar这个包;
把上述三个jar包放到solr的WEB-INF\lib目录下。例如,C:\App\apache-tomcat-9.0.36\webapps\solr\WEB-INF\lib
2、新增data-config.xml文件:
在C:\App\apache-tomcat-9.0.37\webapps\solr\solrhome\core1\conf目录里:
<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<!-- 数据库信息 -->
<dataSource type="JdbcDataSource"
driver="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/books?charactorEncoding=utf-8"
user="root" password="123456" encoding="UTF-8"/>
<document>
<entity name="tb_book_all" pk="id" query="select * from tb_book_all">
<!-- 对应数据库表的字段 -->
<field column="id" name="id" />
<field column="bookname" name="bookname" />
<field column="author" name="author" />
<field column="date" name="date" />
<field column="publisher" name="publisher" />
<field column="price" name="price" />
<field column="url" name="url" />
</entity>
</document>
</dataConfig>
3、修改solrconfig.xml文件:
在solrconfig.xml 中注册 data-config.xml文件信息:
<!-- Request Handlers
http://wiki.apache.org/solr/SolrRequestHandler
Incoming queries will be dispatched to a specific handler by name
based on the path specified in the request.
If a Request Handler is declared with startup="lazy", then it will
not be initialized until the first request that uses it.
-->
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
<!-- SearchHandler
http://wiki.apache.org/solr/SearchHandler
For processing Search Queries, the primary Request Handler
provided with Solr is "SearchHandler" It delegates to a sequent
of SearchComponents (see below) and supports distributed
queries across multiple shards
-->
4、修改managed-schema文件,添加如下内容:
<!-- <field name="id" type="integer" indexed="true" stored="true" required="true" multiValued="false" /> 如果没有这一项请添加 -->
<field name="bookname" type="text_ik" indexed="true" stored="true"/>
<field name="author" type="text_ik" indexed="true" stored="true"/>
<field name="date" type="text_ik" indexed="true" stored="true"/>
<field name="publisher" type="text_ik" indexed="true" stored="true"/>
<field name="price" type="string" indexed="true" stored="true"/>
<field name="url" type="string" indexed="true" stored="true"/>