简体   繁体   English

为什么lucene 2.4中的'删除文档'不起作用?

[英]Why does 'delete document' in lucene 2.4 not work?

I want to delete a document in lucene 2.4 with java. 我想用java删除lucene 2.4中的文档。 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     

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.; 在我看来,最好使用Indexwriter删除文档,因为Indexreader会缓冲删除,并且在调用close()之前不会将更改写入索引。 unless you use the same reference for search. 除非您使用相同的参考进行搜索。

The Lucene wiki states Lucene wiki

Generally it's best to use IndexWriter for deletions, unless 通常,最好使用IndexWriter进行删除,除非

you must delete by document number 您必须按凭证编号删除

you need your searches to immediately reflect the deletions or 您需要搜索才能立即反映删除或

you must know how many documents were deleted for a given deleteDocuments invocation 您必须知道为给定的deleteDocuments调用删除了多少文档

I can see you want the maxdoc value for the document in memory so its a better approach to use Indexwriter 我可以看到你想要内存中文档的maxdoc值,所以它是一个更好的方法来使用Indexwriter

so the answer for your question is 所以问题的答案是

you should close the Indexreader object or use Indexwriter for deletions 您应该关闭Indexreader对象或使用Indexwriter进行删除

maxDoc() won't change until you optimize the index using an IndexWriter . 在使用IndexWriter优化索引之前, maxDoc()不会更改。 At the very least, you need to commit() or your delete may never even make it to disk. 至少,你需要commit()或你的删除甚至可能永远不会到磁盘。

However, numDocs() should return the number of non-deleted documents even before a commit or optimize. 但是, numDocs()应该在提交或优化之前返回未删除文档的数量。

It's probably better practice (and certainly less confusing) to use an IndexWriter to add and delete documents and to open your IndexReader s read-only; 使用IndexWriter添加和删​​除文档并以只读方式打开IndexReader可能是更好的做法(当然也不那么令人困惑); 3.0 will open them read-only by default. 默认情况下,3.0将以只读方式打开它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM