Why does 'delete document' in lucene 2.4 not work?

二次信任 提交于 2019-12-24 02:59:09

问题


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     

回答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.; unless you use the same reference for search.

The Lucene wiki states

Generally it's best to use IndexWriter for deletions, unless

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

I can see you want the maxdoc value for the document in memory so its a better approach to use Indexwriter

so the answer for your question is

you should close the Indexreader object or use Indexwriter for deletions




回答2:


maxDoc() won't change until you optimize the index using an IndexWriter. At the very least, you need to commit() or your delete may never even make it to disk.

However, numDocs() should return the number of non-deleted documents even before a commit or optimize.

It's probably better practice (and certainly less confusing) to use an IndexWriter to add and delete documents and to open your IndexReaders read-only; 3.0 will open them read-only by default.



来源:https://stackoverflow.com/questions/1361213/why-does-delete-document-in-lucene-2-4-not-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!