Solr定时增量更新

一、定时任务执行

很多人利用Windows计划任务,或者Linux的Cron来定期访问增量导入的连接来完成定时增量导入的功能,这其实也是可以的,而且应该没什么问题。 但是更方便,更加与Solr本身集成度高的是利用其自身的定时增量导入功能。

二、配置

1、下载apache-solr-dataimportscheduler.jar放到Tomcat的webapps的solr目录的WEB-INF的lib目录下:

下载链接:http://pan.baidu.com/s/1bpGnqJt

2、修改solr中WEB-INF/web.xml, 在servlet节点前面增加:

<listener>  
          <listener-class>  
                org.apache.solr.handler.dataimport.scheduler.ApplicationListener  
          </listener-class>  
</listener> 

3、 在solr_home\solr下新建conf文件夹,放入下载的dataimport.properties,根据自己的实际需求做相应修改

下载链接: http://pan.baidu.com/s/1dFitqJf

#################################################
#                                               #
#       dataimport scheduler properties         #
#                                               #
#################################################
 
#  to sync or not to sync
#  1 - active; anything else - inactive
syncEnabled=1
 
#  which cores to schedule
#  in a multi-core environment you can decide which cores you want syncronized
#  leave empty or comment it out if using single-core deployment
syncCores=my_core
 
#  solr server name or IP address
#  [defaults to localhost if empty]
server=localhost
 
#  solr server port
#  [defaults to 80 if empty]
port=8089
 
#  application name/context
#  [defaults to current ServletContextListener's context (app) name]
webapp=solr
 
#  URL params [mandatory]
#  remainder of URL
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

4、编辑solr_home\solr\my_core\conf下的db-data-config.xml

<dataConfig>
    <dataSource name="source1"  type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/jfinal_demo?characterEncoding=utf8&amp;zeroDateTimeBehavior=convertToNull" user="root" password="123456"/>

    <span style="white-space:pre">    </span><document>    
        <span style="white-space:pre">    </span>
        <entity name="speech" dataSource="source1"     
                query="select * from  speech"    
                deltaImportQuery="select * from speech where id='${dih.delta.id}'"    
                deltaQuery="select id from speech where create_time &gt; '${dataimporter.last_index_time}'">    
        <!-- name属性,就代表着一个文档,可以随便命名 -->
        <!-- query是一条sql,代表在数据库查找出来的数据 -->
            <!-- 每一个field映射着数据库中列与文档中的域,column是数据库列,name是solr的域(必须是在managed-schema文件中配置过的域才行) -->
            <field column="id" name="s_id"/>
            <field column="content" name="s_content"/>
            <field column="operator" name="s_operator"/>
            <field column="person_synopsis" name="s_person_synopsis"/>
            <field column="person_title" name="s_person_title"/>
        </entity>
    </document>
</dataConfig>


5、重启tomcat,每隔1分钟会进行定时查询并进行增量更新。(这时你可以往数据库新增一条记录,等一分钟后query你会发现多出一条索引)

6、成功更新索引后,会同时更新solr_home\solr\my_core\conf下的dataimport.properties文件

#Mon Aug 07 16:11:13 CST 2017
last_index_time=2017-08-07 16\:11\:13
speech.last_index_time=2017-08-07 16\:11\:13

猜你喜欢

转载自my.oschina.net/u/2963821/blog/1504522