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:
我使用Lucene.Net来索引和搜索文档,我使用以下代码创建或打开索引(如果存在):
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.
现在,如何以简单的方式实现IndexExists?我不需要抛出任何异常。
6 个解决方案
#1
The static method IndexReader.IndexExists(string path) (or one of its overloads) seems pretty suitable.
静态方法IndexReader.IndexExists(字符串路径)(或其重载之一)似乎非常合适。
#2
In < 4.0 is IndexReader.indexExists(org.apache.lucene.store.Directory)
在<4.0中是IndexReader.indexExists(org.apache.lucene.store.Directory)
In > 4.0 is DirectoryReader.indexExists(org.apache.lucene.store.Directory)
在> 4.0中是DirectoryReader.indexExists(org.apache.lucene.store.Directory)
#3
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)
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)
#4
I try to find this anwser too without success and here is how I used in my code:
我试图找到这个anwser也没有成功,这是我在我的代码中使用的方式:
private bool IndexExists { get { return IndexDirectory.FileExists("segments.gen"); } }
private bool IndexExists {get {return IndexDirectory.FileExists(“segments.gen”); }}
#5
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
我知道这是一个旧条目,但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
#6
Whoops!
This is "straight Java" Lucene, but it may well apply to other varieties.
这是“直接Java”Lucene,但它可能适用于其他品种。
In Lucene 4.0.0 the API for DirectoryReader.indexExists()
says
在Lucene 4.0.0中,DirectoryReader.indexExists()的API说
Returns true if an index exists at the specified directory.
如果指定目录中存在索引,则返回true。
But in Lucene 4.10.2 the API for DirectoryReader.indexExists()
says
但是在Lucene 4.10.2中,DirectoryReader.indexExists()的API说
Returns true if an index likely exists at the specified directory. Note that if a corrupt index exists, or if an index in the process of committing
如果索引可能存在于指定目录中,则返回true。请注意,如果存在损坏的索引,或者在提交过程中存在索引
... 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.
......是的,它在句子中断了。 NB我从源代码直接编译了我的Javadoc,但是在在线API中可以看到相同的未完成的短语。不仅如此,我还看了Lucene 6.0.0 API,它完全一样。
The "returns" phrase is however:
然而,“返回”短语是:
true if an index exists; false otherwise
如果索引存在,则为true;否则是假的
... but I currently believe an empty directory will sometimes (?) return true
(from my unit testing). Anyway, I wouldn't trust it.
...但我目前认为空目录有时会(?)返回true(来自我的单元测试)。无论如何,我不相信它。
If you create an IndexReader
on an empty directory, it appears that all its methods will return without throwing exceptions. You can go indexReader.numDocs()
, and this will return 0, but that doesn't prove that there is no index there, only that there are no Document
s. Depending on your requirements that might be enough, of course.
如果在空目录上创建IndexReader,则会显示其所有方法都将返回而不会抛出异常。你可以去indexReader.numDocs(),这将返回0,但这并不能证明那里没有索引,只是没有文档。当然,这取决于您的要求。
Similarly, you can create an IndexSearcher
from such an IndexReader
, and you can create an IndexWriter
. None of these will have any apparent problem with an empty directory.
同样,您可以从这样的IndexReader创建IndexSearcher,并且可以创建IndexWriter。这些都不会对空目录有任何明显的问题。
BETTER SOLUTION:
try {
directoryReader = DirectoryReader.open( fsDir );
} catch ( org.apache.lucene.index.IndexNotFoundException e) {
...
}
This appears, as far as I can tell, to be reliable.
据我所知,这似乎是可靠的。
#1
The static method IndexReader.IndexExists(string path) (or one of its overloads) seems pretty suitable.
静态方法IndexReader.IndexExists(字符串路径)(或其重载之一)似乎非常合适。
#2
In < 4.0 is IndexReader.indexExists(org.apache.lucene.store.Directory)
在<4.0中是IndexReader.indexExists(org.apache.lucene.store.Directory)
In > 4.0 is DirectoryReader.indexExists(org.apache.lucene.store.Directory)
在> 4.0中是DirectoryReader.indexExists(org.apache.lucene.store.Directory)
#3
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)
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)
#4
I try to find this anwser too without success and here is how I used in my code:
我试图找到这个anwser也没有成功,这是我在我的代码中使用的方式:
private bool IndexExists { get { return IndexDirectory.FileExists("segments.gen"); } }
private bool IndexExists {get {return IndexDirectory.FileExists(“segments.gen”); }}
#5
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
我知道这是一个旧条目,但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
#6
Whoops!
This is "straight Java" Lucene, but it may well apply to other varieties.
这是“直接Java”Lucene,但它可能适用于其他品种。
In Lucene 4.0.0 the API for DirectoryReader.indexExists()
says
在Lucene 4.0.0中,DirectoryReader.indexExists()的API说
Returns true if an index exists at the specified directory.
如果指定目录中存在索引,则返回true。
But in Lucene 4.10.2 the API for DirectoryReader.indexExists()
says
但是在Lucene 4.10.2中,DirectoryReader.indexExists()的API说
Returns true if an index likely exists at the specified directory. Note that if a corrupt index exists, or if an index in the process of committing
如果索引可能存在于指定目录中,则返回true。请注意,如果存在损坏的索引,或者在提交过程中存在索引
... 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.
......是的,它在句子中断了。 NB我从源代码直接编译了我的Javadoc,但是在在线API中可以看到相同的未完成的短语。不仅如此,我还看了Lucene 6.0.0 API,它完全一样。
The "returns" phrase is however:
然而,“返回”短语是:
true if an index exists; false otherwise
如果索引存在,则为true;否则是假的
... but I currently believe an empty directory will sometimes (?) return true
(from my unit testing). Anyway, I wouldn't trust it.
...但我目前认为空目录有时会(?)返回true(来自我的单元测试)。无论如何,我不相信它。
If you create an IndexReader
on an empty directory, it appears that all its methods will return without throwing exceptions. You can go indexReader.numDocs()
, and this will return 0, but that doesn't prove that there is no index there, only that there are no Document
s. Depending on your requirements that might be enough, of course.
如果在空目录上创建IndexReader,则会显示其所有方法都将返回而不会抛出异常。你可以去indexReader.numDocs(),这将返回0,但这并不能证明那里没有索引,只是没有文档。当然,这取决于您的要求。
Similarly, you can create an IndexSearcher
from such an IndexReader
, and you can create an IndexWriter
. None of these will have any apparent problem with an empty directory.
同样,您可以从这样的IndexReader创建IndexSearcher,并且可以创建IndexWriter。这些都不会对空目录有任何明显的问题。
BETTER SOLUTION:
try {
directoryReader = DirectoryReader.open( fsDir );
} catch ( org.apache.lucene.index.IndexNotFoundException e) {
...
}
This appears, as far as I can tell, to be reliable.
据我所知,这似乎是可靠的。