views:

51

answers:

0

What is the proper usage pattern for LINQ to Lucene's Index<T>?

It implements IDisposible so I figured wrapping it in a using statement would make the most sense:

IEnumerable<MyDocument> documents = null;

using (Index<MyDocument> index = new Index<MyDocument>(new System.IO.DirectoryInfo(IndexRootPath)))
{
    documents = index.Where(d => d.Name.Like("term")).ToList();
}

I am occasionally experiencing unwanted deleting of the index on disk. It seems happen 100% of the time if multiple instances of the Index exist at the same time. I wrote a test using PLINQ to run 2 searches in parallel and 1 search works while the other returns 0 results because the index is emptied.

  • Am I supposed to use a single static instance instead?
  • Should I wrap it in a Lazy<T>?
  • Am I then opening myself up to other issues when multiple users access the static index at the same time?

I also want to re-index periodically as needed, likely using another process like a Windows service. Am I also going to run into issues if users are searching while the index is being rebuilt?