不少人利用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