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
'.