环境配置 jdk8 solr7.2
jar包准备 solr-dataimporthandler-7.2.1.jar solr-dataimporthandler-extras-7.2.1.jar (这两个从D:\solr-7.2.1\dist 下拷贝)
solr-dataimport-scheduler.jar 最重要,也最难搞的jar,花了我好几个积分现在把地址放给大家
链接:https://pan.baidu.com/s/1MceJrV8BIuCwJM_oDdCRqg
提取码:u9kv
一、将上面的jar放于 D:\solr-7.2.1\server\solr-webapp\webapp\WEB-INF\lib 目录之下。
二、配置D:\solr-7.2.1\server\solr-webapp\webapp\WEB-INF 下的web.xml <servlet>节点之前加入
<listener><listener-class>org.apache.solr.handler.dataimport.scheduler.ApplicationListener</listener-class></listener>监听器
三、在D:\solr-7.2.1\server\solr 下新建文件夹conf 在conf下新建dataimport.properties资源文件,文件内容如下
#################################################
# #
# dataimport scheduler properties #
# #
#################################################
# tosync or not to sync
# 1- active; anything else - inactive
# 这里的配置不用修改
syncEnabled=1
# which cores to schedule
# ina multi-core environment you can decide which cores you want syncronized
# leave empty or comment it out if using single-core deployment
# 修改成你所使用的core,我这里是我自定义的core:simple
syncCores=testcore
# solr server name or IP address
# [defaults to localhost if empty]
# 这个一般都是localhost不会变
server=localhost
# solr server port
# [defaults to 80 if empty]
# 安装solr的tomcat端口,如果你使用的是默认的端口,就不用改了,否则改成自己的端口就好了
port=8983
# application name/context
# [defaults to current ServletContextListener's context (app) name]
# 这里默认不改
webapp=solr
# URL params [mandatory]
# remainder of URL
# 这里改成下面的形式,solr同步数据时请求的链接
params=/dataimport?command=delta-import&clean=false&commit=true
# schedule interval
# number of minutes between two runs
# [defaults to 30 if empty]
#这里是设置定时任务的,单位是分钟,也就是多长时间你检测一次数据同步,根据项目需求修改
# 开始测试的时候为了方便看到效果,时间可以设置短一点
interval=1
# 重做索引的时间间隔,单位分钟,默认7200,即5天;
# 为空,为0,或者注释掉:表示永不重做索引
reBuildIndexInterval=7200
# 重做索引的参数
reBuildIndexParams=/dataimport?command=full-import&clean=true&commit=true
# 重做索引时间间隔的计时开始时间,第一次真正执行的时间=reBuildIndexBeginTime+reBuildIndexInterval*60*1000;
# 两种格式:2012-04-11 03:10:00 或者 03:10:00,后一种会自动补全日期部分为服务启动时的日期
reBuildIndexBeginTime=03:10:00
注意:上面配置,字段前后皆不能多空格,否则定时增量定然不会有效果
四、在自己添加的core下进行相关配置,我的路径为D:\solr-7.2.1\server\solr\testcore\conf
(1)新建data-config.xml资源文件,添加以下内容(我在此处只配置了增量)
column:要与数据库字段一致 updatetime 为修改或者新增等操作的时间,当其值大于上次导入时间,则说明在库里进行了 操作,此时便要更新操作。
<dataConfig>
<!-- 数据源 -->
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/demo"
user="root"
password="root"/>
<document>
<entity name="product" pk="pid" query="SELECT pid,name,catalog_name,price,description,picture,updatetime FROM products" deltaQuery="select pid from products where updateTime>'${dih.last_index_time}'">
<!--deltaImportQuery="select * from products where pid='${dataimporter.delta.id}'" -->
<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" />
<field column="updatetime" name="updatetime"/>
</entity>
<entity name="solrtest" query="select * from solrtest">
<!-- column:数据库的字段名;name:solr的字段名 -->
<field column="id" name="id"/>
<field column="t_name" name="t_name"/>
</entity>
</document>
</dataConfig>
(2)solrconfig.xml下配置一下内容,用于加载data-config.xml
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
(3)managed-schema.xml下配置data-config.xml中数据库与solr库的字段映射(如果solr的主键映射也是为id的字段,可以不用配置id字段,因为solr库默认主键也是id)
type="text_ik"这是我的分词查询字段
<!--配置从数据库导入到sorl中的数据的字段内容,所以每次要从数据库导入什么就需要配置什么-->
<field name="product_name" type="text_ik" indexed="true" stored="true" required="true" multiValued="false"/>
<field name="product_price" type="pint" indexed="true" stored="true"/>
<field name="product_description" type="string" 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="updatetime" type="pdate" indexed="true" stored="true" />
现在基本配置已经全部完成,下面开始进行测试。
五、测试
数据库未进行值操作前:
可以看出,按查询条件检索product_name:"台灯" 只有三条数据,与数据库对比一下
下面我在数据库进行新增一条名为儿童台灯数据
下面我们稍微等一分钟,然后在solr管理页面进行查询
可以看出增量已经更新成功。
千里之行,始于足下。第一次自己写博客,大家多多支持!给个赞哈