问题描述
我想用Java删除Lucene 2.4中的文档.我的代码是
I want to delete a document in lucene 2.4 with java. My code is
Directory directory = FSDirectory.getDirectory("c:/index");
IndexReader indexReader = IndexReader.open(directory);
System.out.println("num="+indexReader.maxDoc());
indexReader.deleteDocuments(new Term("name","1"));
System.out.println("num="+indexReader.maxDoc());
output
num=1
num=1
推荐答案
我认为最好使用Indexwriter删除文档,因为Indexreader 缓冲删除操作,并且不将更改写入到索引直到 close()被调用.除非您使用相同的参考进行搜索.
In my opinion it is best to use Indexwriter to delete the documents, since Indexreader buffers the deletions and does not write changes to the index until close() is called on.; unless you use the same reference for search.
Lucene Wiki 状态
您必须按文件编号删除
您需要搜索以 立即反映出删除内容或
you need your searches to immediately reflect the deletions or
您必须知道多少个文件 已删除给定 deleteDocuments调用
you must know how many documents were deleted for a given deleteDocuments invocation
我可以看到您想要内存中文档的maxdoc值,因此它是使用Indexwriter的更好方法
I can see you want the maxdoc value for the document in memory so its a better approach to use Indexwriter
所以您的问题的答案是
您应该关闭Indexreader对象或使用Indexwriter进行删除
you should close the Indexreader object or use Indexwriter for deletions
这篇关于为什么Lucene 2.4中的“删除文档"不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!