Lucene.NET - check if document exists in index

前提是你 提交于 2019-12-13 18:48:02

问题


I want to check whether or not a document already exists in my Lucene.NET (4.0) index. I have tried using the following code from this post.

IndexReader reader;
Term indexTerm = new Term("filepath", "C:\\my\\path");
TermDocs docs = reader.TermDocs(indexTerm);
if (docs.Next())
{
    continue;
}

But I get an error telling me that reader is unassigned. I have Googled this a lot and cannot find a working answer in Lucene.NET 4 for what should be quite an easy task.

edit: IndexReader is an abstract class. In the documentation it says to call IndexReader.Open() with Lucene.Net.Store.Directory as a parameter, but it itself is abstract. Code samples that I get use it as if it were not. Moreover, in the post I linked to the user said the first segment of code worked.

EDIT2: I now have code that compiles. Here it is:

bool exists = false;
IndexReader reader = IndexReader.Open(Lucene.Net.Store.FSDirectory.Open(lucenePath), false);
Term term = new Term("filepath", "\\myFile.PDF");
TermDocs docs = reader.TermDocs(term);
if (docs.Next())
{
   exists = true;
}

The file myFile.PDF definitely exists, but it always comes back as false. When I look at docs in debug, its Doc and Freq properties state that they "threw an exception of type 'System.NullReferenceException'.


回答1:


You haven't set reader to be anything. You need to initialise it before using it. You can do this is you have the path of the index using:

IndexReader reader = IndexReader.Open(indexDirectoryPath);

or:

Directory directory = FSDirectory.Open(indexDirectoryPath);
IndexReader reader = IndexReader.Open(directory);

or:

DirectoryInfo directoryInfo = new DirectoryInfo(indexDirectoryPath);
Directory directory = FSDirectory.Open(directoryInfo);
IndexReader reader = IndexReader.Open(directory);

where indexDirectoryPath in all cases is the full path of the index location as a string. Which way you use depends on which version of Lucene.Net you are using.

Additionally, make sure that you close the reader when you have finished with it (by calling reader.Close()), otherwise you will likely get file locking issues.



来源:https://stackoverflow.com/questions/20992372/lucene-net-check-if-document-exists-in-index

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