简体   繁体   English

Lucene IndexReader提交不起作用

[英]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. 不推荐使用deleteDocumentcommit方法。 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.

相关问题 Java Lucene IndexReader无法正常工作 - Java Lucene IndexReader not working correctly Lucene NRT-每次提交后都打开一个新的IndexReader? - Lucene NRT - opening a new IndexReader after every commit? IndexReader.getFieldNames Lucene 4 - IndexReader.getFieldNames Lucene 4 在Lucene中替换IndexReader.lastModified - Replacement for IndexReader.lastModified in lucene 如何使用 Lucene IndexReader 查找术语? - How to seek to a term using a Lucene IndexReader? Lucene 热索引备份使用 IndexReader 而不是 IndexWriter/SnapshotDeletionPolicy - Lucene hot index backup using IndexReader instead of IndexWriter/SnapshotDeletionPolicy 如何使用Lucene的shingleanalyzerwrapper + standardanalyzer + indexreader? - How can I use lucene's shingleanalyzerwrapper + standardanalyzer + indexreader? Java-无法将Directory变量作为参数传递给Apache Lucene 6.4.2中的IndexReader.open() - Java-Can't pass Directory variable as an argument to IndexReader.open() in Apache Lucene 6.4.2 既然LucE 4.1中不存在TermEnum,如何从IndexReader中获取字段? - How you do you get fields from an IndexReader now that TermEnum doesnt exist in Lucene 4.1? Lucene 8.5.1 中的 IndexReader.getTermVector(int docID,String field) 中的 docID 是什么,它是如何工作的? - What is docID in IndexReader.getTermVector(int docID ,String field) in Lucene 8.5.1 and how does it work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM