N. Warfield
N. Warfield

Reputation: 346

Using RAMDirectory In Lucene.net causes FileNotFoundException

I have an index manager class that writes documents to my index. When I pass it a RAMDirectory to create an IndexWriter with I'm getting a FileNotFoundException on file segments.gen

Here's my class:

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; }
    }
}

Here's the unit test method:

[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);
        }
    }

I've tried a few different permutations but in this solution I always get a FileNotFoundException. I have another very similar implementation in a test solution which has worked fine. I've also modified this solution a few times to simply declare a new RAMDirectory when I'm creating a new IndexWriter and that also fails.

An help/suggestions are very appreciated. Let me know if I need to clarify anything.

Upvotes: 1

Views: 1806

Answers (2)

N. Warfield
N. Warfield

Reputation: 346

I had enabled break on CLR Exceptions. Lucene was throwing exceptions and would handle them but I was interrupting that process. Once I disabled the CLR Exception break my tests worked successfully using a RAMDirectory.

Upvotes: 2

BlackEcho
BlackEcho

Reputation: 11

A Index Writer will throw a FileNotFound Exception if the create argument is false and Directory does not yet contain an Index. And with a RAMDirectory, the first time an IndexWriter is opened on it, it will not have an index. If you want it to create the index then, you can pass IndexReader.IndexExists(Directory) || recreate to the constructor instead of just recreate.

Another option would be to use one of the IndexWriter constructors that doesn't have a create argument, which will create the index if it doesn't exist, or open the existing one if it does.

Upvotes: 1

Related Questions