簡體   English   中英

Lucene IndexReader提交不起作用

[英]Lucene IndexReader commit not working

我有一種方法可以從我的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();
}

非常感謝瑜伽士

您正在使用哪個Lucene版本? 不推薦使用deleteDocumentcommit方法。 這些操作應該拋出這里提到的IndexWriter

關於您的問題,我認為在IndexSearcher打開時操作索引不是好習慣。 我將從檢查這個方向開始。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM