我有一种方法可以从我的 Lucene 索引中搜索和删除文档。
但是,当我运行代码两次时,它仍然会找到标记为从上一次迭代中删除的文档,并且 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();
}
非常感谢瑜伽士