Lucene.NET-检查索引中是否存在文档 [英] Lucene.NET - check if document exists in index
问题描述
我想检查我的Lucene.NET(4.0)索引中是否已经存在一个文档.我试过使用这篇文章中的以下代码.
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;
}
但是我收到一条错误消息,告诉我reader
未分配.我已经在Google上进行了很多搜索,但是在Lucene.NET 4中找不到有效的答案,这应该是一件很容易的事情.
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.
编辑:IndexReader
是一个抽象类.在文档中说使用Lucene.Net.Store.Directory
作为参数调用IndexReader.Open()
,但是它本身是抽象的.我得到的代码示例好像没有使用它.而且,在与用户链接的帖子中,我说代码的第一段有效.
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;
}
文件myFile.PDF
确实存在,但始终以false返回.当我在调试中查看docs
时,其Doc
和Freq
属性指出它们引发了类型'System.NullReferenceException
'的异常.
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
'.
推荐答案
您尚未将reader
设置为任何值.您需要先对其进行初始化,然后再使用它.您可以这样做是因为您使用以下方法获得了索引的路径:
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);
或:
Directory directory = FSDirectory.Open(indexDirectoryPath);
IndexReader reader = IndexReader.Open(directory);
或:
DirectoryInfo directoryInfo = new DirectoryInfo(indexDirectoryPath);
Directory directory = FSDirectory.Open(directoryInfo);
IndexReader reader = IndexReader.Open(directory);
其中,在所有情况下,indexDirectoryPath
是作为string
的索引位置的完整路径.使用哪种方式取决于您使用的Lucene.Net版本.
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.
此外,请确保在完成阅读器的操作后将其关闭(通过调用reader.Close()
),否则可能会遇到文件锁定问题.
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.
这篇关于Lucene.NET-检查索引中是否存在文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!