为找到多一点的数据,就在百度上搜索了一下电商的模拟数据,其中有一个tb_item表的列如下
配置Solr 连接MySQL,首先在D:\server\solr-7.4.0\dist目录下
复制如下的jar包
复制到
D:\server\solr-7.4.0\server\solr-webapp\webapp\WEB-INF\lib
再到
https://search.maven.org/search?q=mysql-connector下载mysql-connector
复制到D:\server\solr-7.4.0\server\solr-webapp\webapp\WEB-INF\lib目录下
在D:\server\solr-7.4.0\server\solr\lhc_core\conf配置数据库文件信息
<!--数据库配置 -->
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
创建data-config.xml文件
配置data-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource typ="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://120.772.245.224:3307/taotao?useSSL=true&verifyServerCertificate=false&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8"
user="root"
password="lhc123"
/>
<document>
<!--name 使用数据库的表名 -->
<entity dataSource="JdbcDataSource" name="tb_item" query="SELECT id,title,sell_point,price,num,barcode,image, cid,status,created,updated from tb_item; " >
<!--column 是数据库的表的列,name 是solr中的列名 需要在manage-schema中配置规则 -->
<field column="id" name="id"/>
<field column="title" name="product_title"/>
<field column="sell_point" name="product_sell_point"/>
<field column="price" name="product_price"/>
<field column="num" name="product_num"/>
<field column="image" name="product_image"/>
<field column="cid" name="product_cid"/>
<field column="status" name="product_status"/>
<field column="created" name="product_created"/>
<field column="updated" name="product_updated"/>
</entity>
</document>
</dataConfig>
在managed-schema中配置字段检索使用的分词器
<!-- id 使用solr提供的 -->
<field name="product_title" type="text_ik" indexed="true" stored="true" />
<field name="product_sell_point" type="text_ik" indexed="true" stored="true" />
<field name="product_price" type="string" indexed="true" stored="true" />
<field name="product_num" type="string" indexed="true" stored="true" />
<field name="product_image" type="string" indexed="true" stored="true" />
<field name="product_cid" type="string" indexed="true" stored="true" />
<field name="product_status" type="string" indexed="true" stored="true" />
<field name="product_created" type="string" indexed="true" stored="true" />
<field name="product_updated" type="string" indexed="true" stored="true" />
接下来导入数据
execute 后可以不断点击refresh status查看导入的情况,solr导入数据还是很快
接下来就可以点击query查询了
可以看到导入数据成功