前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Solr搜索引擎 — 通过mysql配置数据源

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

作者头像
喵了个咪233
发布2019-05-26 20:16:57
1.5K0
发布2019-05-26 20:16:57
举报

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

附上:

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

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

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

一,准备数据库

数据表结构

代码语言:javascript
复制
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包

代码语言:javascript
复制
> 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会提示找不到配置,复制一份默认的配置文件

代码语言:javascript
复制
> cp -r server/solr/configsets/_default server/solr/new_core

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

代码语言:javascript
复制
> 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:增量索引查询导入的数据
代码语言:javascript
复制
&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行附近,添加与数据库的映射,具体添加规则,不详细写了。

代码语言:javascript
复制
&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

代码语言:javascript
复制
&gt; solr restart -force

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

初始化数据

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

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

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

代码语言:javascript
复制
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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一,准备数据库
  • 二、增加solr-core
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档