0%

solr入门系列3-使用DataImport构建索引

上一篇 中, 我们创建了自己的搜索引擎, 并往其中手动插入了一条数据
在本篇中, 我们将会使用DataImport来构建我们的数据

solrconfig.xml

还记得上一篇 conf 中另一个配置文件 solrconfig.xml
在其中加入

1
2
3
4
5
6
7
8
<!-- 引入dataimporthandler -->
<lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-dataimporthandler-.*\.jar" />

<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

conf 目录下, 创建 data-config.xml, 写入数据源信息, 以及如何构建数据

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/dmhy" user="root" password="admin"/>
<document>
<entity name="publish" transformer="DateFormatTransformer" query="SELECT publish.id, publish.title, fansub.name as fansub_name, fansub.id as fansub_id FROM publish LEFT JOIN fansub ON (publish.fansub_id = fansub.id) WHERE publish.id >= ${dataimporter.request.id}">
<field column="id" name="id"/>
<field column="fansub_name" name="fansub_name"/>
<field column="fansub_id" name="fansub_id"/>
<field column="title" name="title"/>
</entity>
</document>
</dataConfig>

对应数据表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
-- 动画记录表
CREATE TABLE `publish` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`category_id` int(10) unsigned NOT NULL,
`fansub_id` int(10) unsigned NOT NULL,
`title` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`link` varchar(250) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`download_link` varchar(20000) COLLATE utf8_unicode_ci NOT NULL,
`file_size` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`publisher_id` int(10) unsigned NOT NULL,
`publish_time` int(10) unsigned NOT NULL,
`created_at` int(10) unsigned NOT NULL,
`updated_at` int(10) unsigned NOT NULL,
`deleted_at` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `link` (`link`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

-- 字幕组表
CREATE TABLE `fansub` (
`id` int(10) unsigned NOT NULL,
`name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`created_at` int(10) unsigned NOT NULL,
`updated_at` int(10) unsigned NOT NULL,
`deleted_at` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

重启core

DataImport

在solrAdmin中, 打开DataImport, 点击执行

导入成功后, 应该可以正常搜索