Solr搜索引擎 — 通过mysql配置数据源

本文演示了如何配置Solr的MySQL数据源。首先准备数据库,生成测试数据并添加对MySQL使用的jar包;接着增加Solr-core,复制默认配置文件,在solrconfig.xml和managed - schema文件添加配置,重启Solr后可成功增加core,初始化数据后即可查询和更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一节我们已经通过两种方式运行了solr,本节演示怎么配置solr的mysql数据源

附上:

喵了个咪的博客:http://w-blog.cn

Solr官网:http://lucene.apache.org/solr/

> PS:8.0.0版本已经发布,本文使用此时较为稳定的7.7.1版本

一,准备数据库

数据表结构

CREATE TABLE `app` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `app_name` varchar(255) NOT NULL DEFAULT '',
  `score` decimal(10,5) NOT NULL DEFAULT '0.00000',
  `downLoadNum` int(10) NOT NULL DEFAULT '0',
  `top` int(10) NOT NULL DEFAULT '0',
  `type` int(10) NOT NULL DEFAULT '1',
  `update_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;

生成一些测试数据

因为我们需要使用mysql作为数据源,我们需要增加对mysql使用的jar包

> cd server/solr-webapp/webapp/WEB-INF/lib/
> wget http://pic.w-blog.cn/mysql-connector-java.jar

二、增加solr-core

> PS:这里基础solr命令启动的程序并未基于tomcat进行配置,后续cloud集群会使用tomcat进配置

尝试增加一个core会提示找不到配置,复制一份默认的配置文件

> cp -r server/solr/configsets/_default server/solr/new_core

在solrconfig.xml 下添加以下配置,添加位置大约在 680行,SearchHandler 配置上面:

> vim server/solr/new_core/conf/solrconfig.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.
    -->

  <!-- add property -->
  <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
    -->

该文件的配置如下,连接的是mysql也支持其他的数据库

  • query:查询数据库表符合记录数据
  • deltaQuery:增量索引查询主键ID 注意这个只能返回ID字段
  • deltaImportQuery:增量索引查询导入的数据
&gt; vim server/solr/new_core/conf/data-config.xml

<!--?xml version="1.0" encoding="UTF-8"?-->
<dataconfig>
    <datasource name="source" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/appstore" user="root" password="123456" />
    <document>
        <entity name="app" pk="id" datasource="source" query="select * from app" deltaimportquery="select * from app where id = '${dih.delta.id}'" deltaquery="select id from app where update_date > '${dataimporter.last_index_time}' and type = 1">
            <field column="id" name="id" />
            <field column="update_date" name="update_date" />
        </entity>
    </document>
</dataconfig>

在这之后,需要配置managed-schema文件,与数据库进行映射,在117行附近,添加与数据库的映射,具体添加规则,不详细写了。

&gt; vim server/solr/new_core/conf/managed-schema

<!-- add propertity -->
<field name="appName" type="string" indexed="true" stored="true" />
<field name="score" type="string" indexed="true" stored="true" />
<field name="downLoadNum" type="string" indexed="true" stored="true" />
<field name="top" type="string" indexed="true" stored="true" />
<field name="type" type="string" indexed="true" stored="true" />
<field name="update_date" type="string" indexed="true" stored="true" />

重启solr

&gt; solr restart -force

再次增加core发现已经可以增加成功

初始化数据

初始化完成就可以进行查询了

如果修改了可以触发更新操作

当然也可以通过请求URL的方式进行数据更新,这里也方便索引的更新和程序相结合

http://172.16.3.148:8983/solr/new_core/dataimport?command=delta-import&amp;clean=%20false&amp;commit=true&amp;wt=json&amp;indent=true&amp;verbose=false&amp;optimize=false&amp;debug=false

转载于:https://my.oschina.net/wenzhenxi/blog/3052333

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值