[lucene] 更新索引后需要重启服务器的问题!!!
01404421
2009-11-04
在服务器启动的环境下更新了所有索引,但是搜索的结果没有变,必须要服务器重启才能查出新的结果,我的searcher是系统启动是打开然后就不关闭的,是这个原因吗?请问各位是怎么做的,谢谢!
|
|
imjl
2009-11-04
commit
if not then reopen |
|
01404421
2009-11-04
imjl 写道 commit
if not then reopen 高手!能说详细点吗,谢谢了! 是不是reopen Searcher? |
|
imjl
2009-11-04
你更新进去的信息后,,有没有做commit
如果有,那就reopen index |
|
01404421
2009-11-04
imjl 写道 你更新进去的信息后,,有没有做commit
如果有,那就reopen index 谢谢,已经解决了,以前没有用过lucene,我的解决方法如下: boolean neetInit=true;//是否需要初始化search,默认需要 if(search!=null){ neetInit=false; if(!search.getIndexReader().isCurrent()){ search.close(); neetInit=true; } } if(neetInit){ init();//重新初始化search } 网上提到一些方法是从原来的search中拿到indexreader,再用reopen新创建一个search,但是我这里会报indexReader已经关闭的错误(原因还不清楚),所以就重新初始化search |
|
01404421
2009-11-04
问题最终解决了,看了一下API明白了:
先是search.close方法 Note that the underlying IndexReader is not closed, if IndexSearcher was constructed with IndexSearcher(IndexReader r). If the IndexReader was supplied implicitly by specifying a directory, then the IndexReader gets closed 所以我的indexReader会提示已经关闭,因为我已经关闭了searcher。然后是IndexReader的reopen方法: Refreshes an IndexReader if the index has changed since this instance was (re)opened. Opening an IndexReader is an expensive operation. This method can be used to refresh an existing IndexReader to reduce these costs. This method tries to only load segments that have changed or were created after the IndexReader was (re)opened. If the index has not changed since this instance was (re)opened, then this call is a NOOP and returns this instance. Otherwise, a new instance is returned. The old instance is not closed and remains usable. Note: The re-opened reader instance and the old instance might share the same resources. For this reason no index modification operations (e. g. deleteDocument(int), setNorm(int, String, byte)) should be performed using one of the readers until the old reader instance is closed. Otherwise, the behavior of the readers is undefined. You can determine whether a reader was actually reopened by comparing the old instance with the instance returned by this method: IndexReader reader = ... ... IndexReader new = r.reopen(); if (new != reader) { ... // reader was reopened reader.close(); } reader = new; 然后我把自己的代码做了一下修改: if(search!=null){ if(!search.getIndexReader().isCurrent()){ IndexReader ir=search.getIndexReader().reopen(); search.close(); search=new IndexSearcher(ir); } } 这样就可以了。Over,下班喽回家睡觉,哈哈 |
相关讨论
相关资源推荐
- java sqlserver 异常处理_SQLServerException
- javaEE连接SQL server2005采用dbutils插入时报错
- mysql jdbc常见错误_jdbc+mysql常见报错总结
- java jdbc连接oracle数据库连接 不抛出异常_JDBC连接Oracle数据库常见问题及解决方法...
- 数据源,分层开发以及jsp标签总结及相关代码
- Java数据库开发与应用之MySQL数据库、JDBC操作数据库、C3P0数据库连接池,Java反射等
- jsp struts 编程常见错误
- 整理对应_JSP第二版课后习题答案【侵权联系我删除】
- jsp 连接mysql空指针_程序开发常见错误
- SQL注入+利用详解