如果想要知道如何安装solr,集成IKAnalyzer中文分词器,批量导入数据库数据,java使用参照以下本博主博文:

安装solr


集成IKAnalyzer中文分词器


solr使用浏览器批量导入数据库中数据


solr在java中的案例


1.依赖包下载

链接:https://pan.baidu.com/s/1z9kA1eGrorQGzhmILQxVFQ 

提取码:9igo 

solr使用网页浏览器批量导入数据库中数据(本案例是mysql)_solr

1为mysql驱动,2为dataimprt包

2.将以上3个包复制到solrhome/collection1/lib,如果没有这个文件夹(/lib文件夹),请自行创建(我是自己创建的)

solr使用网页浏览器批量导入数据库中数据(本案例是mysql)_solr_02

3.配置solrconfig.xml,添加一个requestHandler,文件位置solrhome/collection1/conf。

solr使用网页浏览器批量导入数据库中数据(本案例是mysql)_导入_03

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

添加位置,我放在/select上面(不限制位置),此处有个<str name="config">data-config.xml</>

solr使用网页浏览器批量导入数据库中数据(本案例是mysql)_导入_04

在测试此之前,需要在schema.xml中添加新的域,文件位置solrhome/collection1/conf/schema.xml

<!--product-->
<field name="product_name" type="text_ik" indexed="true" stored="true"/>
<field name="product_price" type="float" indexed="true" stored="true"/>
<field name="product_description" type="text_ik" indexed="true" stored="false" />
<field name="product_picture" type="string" indexed="false" stored="true" />
<field name="product_catalog_name" type="string" indexed="true" stored="true" />

<field name="product_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="product_name" dest="product_keywords"/>
<copyField source="product_description" dest="product_keywords"/>

solr使用网页浏览器批量导入数据库中数据(本案例是mysql)_solr_05

还有就是将lucene.sql文件导入mysql中

solr使用网页浏览器批量导入数据库中数据(本案例是mysql)_dataimport_06

solr使用网页浏览器批量导入数据库中数据(本案例是mysql)_dataimport_07

我这里使用的是navicat打开的。

再就是说需要创建一个数据库连接,以及数据库查询的xml文件,文件放置位置同上面文件位置solrhome/collection1/conf

solr使用网页浏览器批量导入数据库中数据(本案例是mysql)_solr_08

 

数据库data-config.xml内容:

<?xml version="1.0" encoding="UTF-8" ?>  
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/solr"
user="root"
password="root"/>
<document>
<entity name="product" query="SELECT pid,name,catalog_name,price,description,picture FROM products ">
<field column="pid" name="id"/>
<field column="name" name="product_name"/>
<field column="catalog_name" name="product_catalog_name"/>
<field column="price" name="product_price"/>
<field column="description" name="product_description"/>
<field column="picture" name="product_picture"/>
</entity>
</document>
</dataConfig>

solr使用网页浏览器批量导入数据库中数据(本案例是mysql)_xml_09

4.重启tomcat

点击execute导入数据库中的数据,验证是否成功,此处我导入成功3803条记录

solr使用网页浏览器批量导入数据库中数据(本案例是mysql)_导入_10