lucene实时 增量索引_Solr搜索引擎 — 通过mysql配置数据源

5328cd9c0423036556e131c0bc860ff1.png

上一节我们已经通过两种方式运行了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;

生成一些测试数据

e1b6fb9e57b4bcc2792b2e743ac7fb38.png

因为我们需要使用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进配置

79c703b853a96d24afe8a576a43e2e30.png

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

330e5ffaa361c8662fcca4ef78c76051.png
> 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:增量索引查询导入的数据
> 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行附近,添加与数据库的映射,具体添加规则,不详细写了。

> 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

> solr restart -force

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

98cecc2820df459818c5f9f21af3d720.png

31e11e0fa33ce75c443358f87610dec5.png

初始化数据

0249c5ee2c8154424b3ba593b13b2d39.png

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

5a7460d1c9c58b4863abaecfe4a569a7.png

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

2030ddef190cc068600f209a97db483f.png

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

http://172.16.3.148:8983/solr/new_core/dataimport?command=delta-import&clean=%20false&commit=true&wt=json&indent=true&verbose=false&optimize=false&debug=false
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值