我有一个索引管理类,它将文档写入我的索引。当我传递一个RAMDirectory来创建一个IndexWriter时,我在文件segments.gen上得到一个FileNotFoundException
这是我的课:
public class IndexManager
{
private readonly IIndexPersistable _indexPersister;
public IndexManager(IIndexPersistable indexPersister)
{
_indexPersister = indexPersister;
}
public Directory Directory
{
get { return _indexPersister.Directory; }
}
internal void WriteDocumentsToIndex(
IEnumerable<Document> documents,
bool recreateIndex)
{
using(var writer =
new IndexWriter(
Directory,
new StandardAnalyzer(LuceneVersion.LUCENE_30),
recreateIndex,
IndexWriter.MaxFieldLength.UNLIMITED))
{
foreach (Document document in documents)
{
writer.AddDocument(document);
}
writer.Optimize();
}
}
}
public class InMemoryPersister : IIndexPersistable
{
private readonly Directory _directory;
public InMemoryPersister()
{
_directory = new RAMDirectory();
}
public Directory Directory
{
get { return _directory; }
}
}下面是单元测试方法:
[TestMethod]
public void TestMethod1()
{
using (var manager = new IndexManager(new InMemoryPersister()))
{
IList<Recipe> recipes = Repositories.RecipeRepo.GetAllRecipes().ToList();
IEnumerable<Document> documents = recipes.Select(RecipeIndexer.IndexRecipe);
manager.WriteDocumentsToIndex(documents, true);
}
}我尝试过几个不同的排列,但在这个解决方案中,我总是得到一个FileNotFoundException。在测试解决方案中,我还有另一个非常类似的实现,它运行得很好。我还对这个解决方案做了几次修改,以便在创建新的RAMDirectory时声明一个新的IndexWriter,但这也失败了。
非常感谢你的帮助/建议。如果我需要澄清什么,请告诉我。
发布于 2015-03-05 23:23:58
我已经启用了CLR异常的中断。Lucene抛出异常并处理它们,但我打断了这个过程。一旦我禁用了CLR异常中断,我的测试就可以使用RAMDirectory成功地工作。
发布于 2013-10-11 23:46:07
如果创建参数为false,且目录尚未包含索引,则索引写入器将引发FileNotFound异常。对于RAMDirectory,第一次在其上打开IndexWriter时,它将没有索引。如果希望它创建索引,则可以将IndexReader.IndexExists(Directory) || recreate传递给构造函数,而不仅仅是recreate。
另一种选择是使用不带create参数的IndexWriter构造函数之一,如果它不存在,它将创建索引;如果不存在,则打开现有的索引。
https://stackoverflow.com/questions/19061321
复制相似问题