tags:

views:

271

answers:

2

How can I use IndexSearcher so that it will not lock the index and open it in read only mode?

Right now I just have

var searcher = new IndexSearcher(LuceneIndexPath);

I have been reading that opening in read-only mode boosts performance so I was wondering how to go about it. I could not find a lot of documentation on this subject.

+3  A: 

If that's all you have, you should worry about performance later. Here are a couple of tips to get you going with Lucene before you resort to fiddling with index permissions:

  • Lucene is not a database; it's an index. All fields other than unindexed get minimized in a very effective way. When a search is made, the query is also minimized such that it will match those unique hashes of the data. Anything that you're not storing to be searched should be something you're going to use to recover information from a DB. This can decrease your index size (and thus search speed) by an order of magnitude.
  • Stem everything -- use somethign even as simplistic as the Porter Stemmer to reduce the length of the words in your text. When you make a query, stem that, too. While this has a small impact on the size of your index and the speed of querying, this also increases your search robustness, which is just as important.
  • Stopwords? Who needs 'em? Seriously, find yourself a good list of stopwords and remove them from any field you plan on indexing. The most frequent terms you'll find in any English text are absolutely worthless with respect to information retrieval. That being said, if you're storing them, your database is probably very needlessly big. Imagine taking a walk across equally sized letters written on the sidewalk. How long would "The Good, The Bad, and the Ugly" take compared to "Good, Bad, Ugly"?

Make sure that these three major aspects are addressed first, and you'll likely not need to worry that much about performance. Worrying about performance before these are addressed would be one of those notoriously evil "premature optimizations".

Robert Elwell
+2  A: 

I read about this recently and came up with this way to use an indexsearcher in a readonly fashion leaving the reader open for the shortest time possible:

    private T searchIndex<T>(Func<IndexSearcher, T> searchAction)
{
 var indexReader = IndexReader.Open(_indexDirectory, true);
 var indexSearcher = new IndexSearcher(indexReader);

 var result = searchAction(indexSearcher);

 indexSearcher.Close();
 indexReader.Close();

 return result;
}

consuming code looks something like this

var hits = searchIndex(s=> {
   var query = _queryParser.Parse(searchString);
   return indexSearcher.Search(query);
});
KevM