一、定時任務執行
很多人利用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&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 > '${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