问题
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