如何判断文件夹中是否存在 Lucene.Net 索引?
我正在使用 Lucene.Net 来索引和搜索文档,并且使用以下代码来创建或打开索引(如果存在):
IndexWriter writer = new IndexWriter(@"C:\index", new StandardAnalyzer(), !IndexExists);
...
private bool IndexExists
{
get
{
return ??
}
}
现在,如何以简单的方式实现 IndexExists
? 我不需要抛出任何异常。
I am using Lucene.Net for indexing and searching documents, and I am using the following code to create or open an index if one exists:
IndexWriter writer = new IndexWriter(@"C:\index", new StandardAnalyzer(), !IndexExists);
...
private bool IndexExists
{
get
{
return ??
}
}
Now, how can implement IndexExists
in a simple way? I don't need any exceptions to be thrown.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
静态方法 IndexReader.IndexExists(string path) (或其重载之一)似乎非常合适。
The static method IndexReader.IndexExists(string path) (or one of its overloads) seems pretty suitable.
在< 4.0 是
IndexReader.indexExists(org.apache.lucene.store.Directory)
中的 > 4.0 是 DirectoryReader.indexExists(org.apache.lucene.store.Directory)
In < 4.0 is
IndexReader.indexExists(org.apache.lucene.store.Directory)
In > 4.0 is
DirectoryReader.indexExists(org.apache.lucene.store.Directory)
您可以只使用不带布尔参数的构造函数。 如果存在索引,则将打开一个现有索引;如果不存在,则创建一个新索引。
Java 文档链接(Lucene.Net 相同):http://lucene.apache.org/java/2_3_1/api/org/apache/lucene/index/IndexWriter.html#IndexWriter(org.apache.lucene. store.Directory,org.apache.lucene.analysis.Analyzer)
You could just use the constructor that doesn't take a boolean param. That will open an existing index if there is one there or create a new one if it doesn't exist.
Java documentation link (same for Lucene.Net):
http://lucene.apache.org/java/2_3_1/api/org/apache/lucene/index/IndexWriter.html#IndexWriter(org.apache.lucene.store.Directory, org.apache.lucene.analysis.Analyzer)
我也尝试找到这个 anwser,但没有成功,这是我在代码中使用的方式:
private bool IndexExists
{
得到
{
return IndexDirectory.FileExists("segments.gen");
}
}
I try to find this anwser too without success and here is how I used in my code:
private bool IndexExists
{
get
{
return IndexDirectory.FileExists("segments.gen");
}
}
我知道这是一个旧条目,但 Sean Carpenter 发布的内容完全正确,并且这个构造函数甚至在最新版本的 Lucene .NET 中也存在。 IndexWriter 类的文档可以在这里找到:
http://lucenenet.apache.org/docs/3.0 .3/d2/d1d/class_lucene_1_1_net_1_1_index_1_1_index_writer.html#af4620c14320934601058e0e9cac9bfab
I know that this is an old entry, but what Sean Carpenter posted is totally right and this constructor exists even in the latest version of Lucene .NET. The documentation for the IndexWriter class can be found here:
http://lucenenet.apache.org/docs/3.0.3/d2/d1d/class_lucene_1_1_net_1_1_index_1_1_index_writer.html#af4620c14320934601058e0e9cac9bfab
哎呀!
这是“纯 Java”Lucene,但它很可能适用于其他品种。
在 Lucene 4.0.0 中,
DirectoryReader.indexExists()
的 API 表示但在 Lucene 4.10.2 中,
DirectoryReader.indexExists()
的 API 说……是的,它在句子中间中断。 注意:我直接从源代码编译了 Javadoc,但在在线 API 中可以看到相同的未完成短语。 不仅如此,我查看了Lucene 6.0.0 API,也是一模一样的。
然而,“返回”短语是:
...但我目前相信空目录有时会(?)返回
true
(来自我的单元测试)。 无论如何,我不会相信它。如果您在空目录上创建一个
IndexReader
,那么它的所有方法似乎都会返回而不抛出异常。 你可以使用indexReader.numDocs()
,这将返回0,但这并不能证明那里没有索引,只是证明没有Document
。 当然,根据您的要求,这可能就足够了。同样,您可以从这样的
IndexReader
创建一个IndexSearcher
,并且可以创建一个IndexWriter
。 对于空目录来说,这些都不会有任何明显的问题。更好的解决方案:
据我所知,这似乎是可靠的。
Whoops!
This is "straight Java" Lucene, but it may well apply to other varieties.
In Lucene 4.0.0 the API for
DirectoryReader.indexExists()
saysBut in Lucene 4.10.2 the API for
DirectoryReader.indexExists()
says... yes, it breaks off mid-sentence. NB I compiled my Javadoc direct from the source, but the same unfinished phrase can be seen in the online API. Not only that, but I looked at the Lucene 6.0.0 API, and it is exactly the same.
The "returns" phrase is however:
... but I currently believe an empty directory will sometimes (?) return
true
(from my unit testing). Anyway, I wouldn't trust it.If you create an
IndexReader
on an empty directory, it appears that all its methods will return without throwing exceptions. You can goindexReader.numDocs()
, and this will return 0, but that doesn't prove that there is no index there, only that there are noDocument
s. Depending on your requirements that might be enough, of course.Similarly, you can create an
IndexSearcher
from such anIndexReader
, and you can create anIndexWriter
. None of these will have any apparent problem with an empty directory.BETTER SOLUTION:
This appears, as far as I can tell, to be reliable.