不少人利用Windows計劃任務,或者Linux的Cron來按期訪問增量導入的鏈接來完成定時增量導入的功能,這其實也是能夠的,並且應該沒什麼問題。 可是更方便,更加與Solr自己集成度高的是利用其自身的定時增量導入功能。mysql
一、下載apache-solr-dataimportscheduler.jar放到Tomcat的webapps的solr目錄的WEB-INF的lib目錄下:web
下載連接:http://pan.baidu.com/s/1bpGnqJtsql
二、修改solr中WEB-INF/web.xml, 在servlet節點前面增長:數據庫
<listener> <listener-class> org.apache.solr.handler.dataimport.scheduler.ApplicationListener </listener-class> </listener>
三、 在solr_home\solr下新建conf文件夾,放入下載的dataimport.properties,根據本身的實際需求作相應修改apache
下載連接: http://pan.baidu.com/s/1dFitqJftomcat
################################################# # # # 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
四、編輯solr_home\solr\my_core\conf下的db-data-config.xmlapp
<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>
五、重啓tomcat,每隔1分鐘會進行定時查詢並進行增量更新。(這時你能夠往數據庫新增一條記錄,等一分鐘後query你會發現多出一條索引)webapp
六、成功更新索引後,會同時更新solr_home\solr\my_core\conf下的dataimport.properties文件ide
#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