Solr5.5.5索引增量更新

Solr索引的定时增量更新需要加入一个jar包,solr5所需jar包下载源码下载

1、将 apache-solr-dataimportscheduler-1.0.jar 和solr自带的 apache-solr-dataimporthandler-.jar, apache-solr-dataimporthandler-extras-.jar 放到solr的lib目录下面 。

注意:使用apache-solr-dataimportscheduler-1.0.jar时,发现tomact启动不起来, Solr管理界面报404错误。后来查看了tomcat日志,发现报找不到dihs.jar。所以要把apache-solr-dataimportscheduler-1.0.jar名称改为dihs.jar。

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

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

3、将apache-solr-dataimportscheduler-.jar 中 dataimport.properties 取出

      将dataimport.properties下载并该文件并根据实际情况修改,然后放到 solr.home/conf (不是solr.home/core/conf) 目录下面,该目录是没有的,需要新建 

# dataimport.properties example
#
# From this example, copy everything bellow "dataimport scheduler properties" to your
#   dataimport.properties file and then change params to fit your needs
#
# IMPORTANT:
# Regardless of whether you have single or multiple-core Solr,
#   use dataimport.properties located in your solr.home/conf (NOT solr.home/core/conf)
# For more info and context see here:
# http://wiki.apache.org/solr/DataImportHandler#dataimport.properties_example


#Tue Jul 21 12:10:50 CEST 2010
#metadataObject.last_index_time=2010-09-20 11\:12\:47
#last_index_time=2010-09-20 11\:12\:47


#################################################
#                                               #
#       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=mycore

#  solr server name or IP address
#  [defaults to localhost if empty]
server=localhost

#  solr server port
#  [defaults to 80 if empty]
port=8080

#  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=/select?qt=/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、重启tomcat,向数据库添加一条记录。

注意:由于在conf下data-config.xml文件中内容如下:

<dataConfig>
    <!-- 这是mysql的配置,学会jdbc的都应该看得懂 -->
    <dataSource name="solrDB" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" 
	url="jdbc:mysql://127.0.0.1:3306/queans" 
	user="root" 
	password="bulei123"/>
    <document>
        <!-- name属性,就代表着一个文档,可以随便命名 -->
        <!-- query是一条sql,代表在数据库查找出来的数据 -->
        <entity dataSource="solrDB" name="ajtable" pk="ajid"  
		query="SELECT CONCAT('Aj',ajid) ajid,ajdz,'案件信息' as tbName from ajtable"
		deltaImportQuery="select CONCAT('Aj',ajid) ajid,ajdz,'案件信息' as tbName from ajtable where ajid='${dih.delta.ajid}'"    
        deltaQuery="select ajid from ajtable where updateTime>= '${dih.last_index_time}'">
            <!-- 每一个field映射着数据库中列与文档中的域,column是数据库列,name是solr的域(必须是在managed-schema文件中配置过的域才行) -->
            <field column="ajid" name="id"/>
            <field column="ajdz" name="ajdz"/>
	    <field column="tbName" name="tbName"/>
        </entity>
    </document>
</dataConfig>

可以看出是根据updateTime时间来增量更新索引的,所以新数据的updateTime要大于apache-tomcat-8.0.9\webapps\solr\solrhome\myindex\conf目录下dataimport.properties文件中的last_index_time的时间值。

#Wed Mar 07 17:03:24 CST 2018
last_index_time=2018-03-07 17\:03\:24
ajtable.last_index_time=2018-03-07 17\:03\:24 

参考博客:http://blog.csdn.net/a491857321/article/details/52483860

猜你喜欢

转载自blog.csdn.net/u013517229/article/details/79473979