将数据库中的数据导入Solr索引库

xiaoxiao2021-02-28  82

在前面的文章中介绍了solr的一些配置和功能,这篇文件开始讲如何具体使用solr。


将数据库中的数据导入索引库

在大部分应用中,主要还是使用的是数据库中的数据,因此,这一步还是非常重要的。 现在目录结构如图所示: 在solr后台管理界面中 dataimport 负责将数据库中数据导入到索引库中,不过在导入之前,还需要一些相关配置。

1、需要的jar包

还需要mysql的驱动包

将这3个jar包 放入 E:\solr\solrhome\collection1\lib 下

2.在solrconfig.xml中最后面添加一个requesthandler节点

<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 是指关于要导入的数据库的配置信息。

2、在E:\solr\solrhome\collection1\conf 下创建 data-config.xml 文件

<?xml version="1.0" encoding="UTF-8" ?> <dataConfig> <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/lucene" 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>

数据库和账户密码根据自己的实际情况来写 可以看出,可以在

query="SELECT pid,name,catalog_name,price,description,picture FROM products "

指定自己索引库中要存的索引,

<field column="pid" name="id"/> <field column="name" name="product_name"/>

可以配置索引中的域名 和 数据库字段的映射关系 ,其中column为字段名,name为域名。

3、在schema.xml中配置自定义域 首先要配置好ik分词器,不会的可以参考下面这篇文章 solr的安装与使用(二)

然后在后面增加自定义域

<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"/>

这里面name 和 data-config.xml中的 name一致。type使用ik分词器定义的type

type="text_ik"

4、重启tomcat 选中collection1 点击dataimport 5、点击执行,就可以将数据中数据导入索引库了。

转载请注明原文地址: https://www.6miu.com/read-62668.html

最新回复(0)