solr4.7.1定时实时重建索引和增量更新

Solr官方提供了很强大的Data Import Request Handler,同时提供了一个简单的 Scheduler,示例中的 Scheduler 只支持增量更新,不支持定期重做索引,因此我做了一个简单的封装,增加了重做索引的定时器。

        1. 将 apache-solr-dataimportscheduler-1.0.jar 和solr自带的 solr-dataimporthandler-4.7.1.jar、 solr-dataimporthandler-extras-4.7.1.jar 放到tomcat发布的webapps/solr/WEB-INF/lib目录下面(或者放到solr.war的lib目录下面)。
        2.修改tomcat发布的webapps/solr/WEB-INF/web.xml(或者修改solr.war中WEB-INF/web.xml), 在servlet节点前面增加:

[javascript]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <listener>  
  2.        <listener-class>org.apache.solr.handler.dataimport.scheduler.ApplicationListener</listener-class>  
  3. </listener>  
        3.将apache-solr-dataimportscheduler-1.0-source.jar 中 dataimport.properties 取出并根据实际情况修改,然后放到 solr.home/conf (不是solr.home/core/conf) 目录下面,conf这个目录不存在,需要自己手动创建该目录即可。

        4.重启tomcat或者jboss 即可。


dataimport.properties 配置项说明

[javascript]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #################################################  
  2. #                                               #  
  3. #       dataimport scheduler properties         #  
  4. #                                               #  
  5. #################################################  
  6.  
  7. #  to sync or not to sync  
  8. #  1 - active; anything else - inactive  
  9. syncEnabled=1  
  10.  
  11. #  which cores to schedule  
  12. #  in a multi-core environment you can decide which cores you want syncronized  
  13. #  leave empty or comment it out if using single-core deployment  
  14. syncCores=core0,core1  
  15.  
  16. #  solr server name or IP address  
  17. #  [defaults to localhost if empty]  
  18. server=localhost  
  19.  
  20. #  solr server port  
  21. #  [defaults to 80 if empty]  
  22. port=8080  
  23.  
  24. #  application name/context  
  25. #  [defaults to current ServletContextListener's context (app) name]  
  26. webapp=solr  
  27.  
  28. #  URL params [mandatory]  
  29. #  remainder of URL  
  30. params=/dataimport?command=delta-import&clean=false&commit=true  
  31.  
  32. #  schedule interval  
  33. #  number of minutes between two runs  
  34. #  [defaults to 30 if empty]  
  35. #  增量索引的时间间隔,单位分钟  
  36. #  为空,为0,或者注释掉:表示永不增量索引  
  37. interval=1  
  38.  
  39. #  重做索引的时间间隔,单位分钟,默认7200,即5天;   
  40. #  为空,为0,或者注释掉:表示永不重做索引  
  41. reBuildIndexInterval=2  
  42.  
  43. #  重做索引的参数  
  44. reBuildIndexParams=/dataimport?command=full-import&clean=false&commit=true  
  45.  
  46. #  重做索引时间间隔的计时开始时间,第一次真正执行的时间=reBuildIndexBeginTime+reBuildIndexInterval*60*1000;  
  47. #  两种格式:2012-04-11 03:10:00 或者  03:10:00,后一种会自动补全日期部分为服务启动时的日期  
  48. reBuildIndexBeginTime=03:10:00  


 

        对于以上配置,发现两个bug,一个是如果reBuildIndexBeginTime的值为空,则会导致interval强转时报错,另一个是请求solr服务器时返回的状态码永远是415(对于当前请求的方法和所请求的资源,请求中提交的实体并不是服务器中所支持的格式,因此请求被拒绝。)

        1、第一个bug


        这一块代码直接导致一下方法调用的时候报强转错,


        这个方法对interval属性进行了强转,


        看如果reBuildIndexBeginTime为空时的设置的代码:


        以上红框是更改后的格式,原格式只有“yyyy-MM-dd”部分,这样会导致启动的时候会不停地http请求solr服务器,因此需加“HH:mm:ss”,以此解决问题。
        第一个bug解决方法把图1中画红框的代码去除就行了。

        2、第二个bug


        导致原因是HTTP以post请求,我们知道dataimport.properties的param是以get方式的链接,参数也包含在里面,程序未对此链接形式做处理封装成post提交方式,所以导致请求solr失败,把Http链接方式改成get就行了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值