[英]Lucene IndexReader commit not working
I have a method that searches and delete documents from my Lucene index. 我有一种方法可以从我的Lucene索引中搜索和删除文档。
However, when I run the code twice, it still finds the documents that where marked to be deleted from the previous iteration, and indexReader.hasDeletions() evaluates true. 但是,当我运行两次代码时,它仍然会找到标记为从上一次迭代中删除的文档,并且indexReader.hasDeletions()的评估结果为true。
public void duplicatesRemover(String currentIndex) throws Exception {
Directory directory = FSDirectory.open(new File(currentIndex));
IndexReader indexReader = IndexReader.open(directory, false);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
int dups = 0;
for (int i = 0; i < indexReader.numDocs(); i++) {
Document doc = indexReader.document(i);
int articleId = Integer.parseInt(doc.get("articleId"));
Query q = NumericRangeQuery.newIntRange("articleId", articleId, articleId, true, true);
TopDocs topDocs = indexSearcher.search(q, 10);
if (topDocs.totalHits > 1 ) {
indexReader.deleteDocument(i);
System.out.print("Total matches from search found: " + topDocs.totalHits + " articleId = " + articleId);
System.out.println(" total dups found " + ++dups + "/" + i);
}
}
if(indexReader.hasDeletions()){
System.out.println("Has deletions");
Map<String, String> commitUserData = new HashMap<String, String>();
commitUserData.put("foo", "fighter");
indexReader.commit(commitUserData);
}
indexSearcher.close();
indexReader.close();
directory.close();
}
Many thanks yogi 非常感谢瑜伽士
What Lucene version are you using? 您正在使用哪个Lucene版本? The deleteDocument
and commit
methods are deprecated. 不推荐使用deleteDocument
和commit
方法。 Those actions should be done threw an IndexWriter
as mentioned here . 这些操作应该抛出这里提到的IndexWriter
。
Regarding your problem i don't think it is good practice to manipulate the index while an IndexSearcher
is open. 关于您的问题,我认为在IndexSearcher
打开时操作索引不是好习惯。 I would start by checking this direction. 我将从检查这个方向开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.