简体   繁体   English

Lucene.NET-检查索引中是否存在文档

[英]Lucene.NET - check if document exists in index

I want to check whether or not a document already exists in my Lucene.NET (4.0) index. 我想检查我的Lucene.NET(4.0)索引中是否已经存在一个文档。 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. 但是我收到一条错误消息,告诉我reader未分配。 I have Googled this a lot and cannot find a working answer in Lucene.NET 4 for what should be quite an easy task. 我已经用Google搜索了很多,但是在Lucene.NET 4中找不到有效的答案,因为这应该是一件容易的事。

edit : IndexReader is an abstract class. 编辑IndexReader是一个抽象类。 In the documentation it says to call IndexReader.Open() with Lucene.Net.Store.Directory as a parameter, but it itself is abstract. 在文档中,它说使用Lucene.Net.Store.Directory作为参数调用IndexReader.Open() ,但它本身是抽象的。 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. EDIT2:我现在有可编译的代码。 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. 文件myFile.PDF确实存在,但始终以false返回。 When I look at docs in debug, its Doc and Freq properties state that they "threw an exception of type ' System.NullReferenceException '. 当我在调试中查看docs时,其DocFreq属性指出它们“引发了类型为' System.NullReferenceException '的异常”。

You haven't set reader to be anything. 您尚未将reader设置为任何东西。 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 . 其中indexDirectoryPath在所有情况下都是string形式的索引位置的完整路径。 Which way you use depends on which version of Lucene.Net you are using. 使用哪种方式取决于您使用的Lucene.Net版本。

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. 此外,请确保在完成阅读器的操作后将其关闭(通过调用reader.Close() ),否则可能会遇到文件锁定问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM