Lucene UpdatedOcument删除了该文档,但计数不断增加



我正在使用updateDocument()方法在Lucene索引中更新我的文档。这就是我的方式。

writer.updateDocument(new Term(Constants.DOC_ID_FIELD, doc.get(Constants.DOC_ID_FIELD)), doc);

我使用卢克(Luke)检查了我的索引数据,并在索引的第二次运行中发现了Deleted Document - not available。因此,基本上,该文档被标记为已删除,但仍位于索引中。

我不想保留这些标记的已删除文档。我做错了吗?

另外,我的理解是,当我更新文档时,它会删除旧文档,然后添加新文档。是不是吗?

从邮件列表中获得答案。

IndexWriter.updateDocument()删除,然后添加。因此,您的索引将删除文档。你为什么在乎?随着部分合并,他们最终会消失。

如果您真的很在乎,请参见IndexWriter,forceMergeDeletes()。也可以看看 Javadoc为此:这通常是一个昂贵的操作;它很少有必要。

以下应从索引中删除文档:

public static void deleteDocumentsFromIndexUsingTerm(Term term) throws IOException, ParseException {
        System.out.println("Deleting documents with field '" + term.field() + "' with text '" + term.text() + "'");
        Directory directory = FSDirectory.getDirectory(INDEX_DIRECTORY);
        IndexReader indexReader = IndexReader.open(directory);
        indexReader.deleteDocuments(term);
        indexReader.close();
    }

参考:http://www.avajava.com/tutorials/lessons/how-do-i-i-i-delete-a-a-document-from-a-a-lucene-index-us-ash-------- in-lucene-index-using-using-the-value of-a-field-a-field.html

最新更新