private void ValidateSearcher(bool forceReopen) { EnsureIndex(); if (!forceReopen) { if (_searcher == null) { lock (Locker) { //double check if (_searcher == null) { try { _searcher = new IndexSearcher(GetLuceneDirectory(), true); } catch (IOException ex) { throw new ApplicationException("Could not create an index searcher with the supplied lucene directory", ex); } } } } else { if (_searcher.GetReaderStatus() != ReaderStatus.Current) { lock (Locker) { //double check, now, we need to find out if it's closed or just not current switch (_searcher.GetReaderStatus()) { case ReaderStatus.Current: break; case ReaderStatus.Closed: _searcher = new IndexSearcher(GetLuceneDirectory(), true); break; case ReaderStatus.NotCurrent: //yes, this is actually the way the Lucene wants you to work... //normally, i would have thought just calling Reopen() on the underlying reader would suffice... but it doesn't. //here's references: // http://stackoverflow.com/questions/1323779/lucene-indexreader-reopen-doesnt-seem-to-work-correctly // http://gist.github.com/173978 var oldReader = _searcher.GetIndexReader(); var newReader = oldReader.Reopen(true); if (newReader != oldReader) { _searcher.Close(); oldReader.Close(); _searcher = new IndexSearcher(newReader); } break; } } } } } else { //need to close the searcher and force a re-open if (_searcher != null) { lock (Locker) { //double check if (_searcher != null) { try { _searcher.Close(); } catch (IOException) { //this will happen if it's already closed ( i think ) } finally { //set to null in case another call to this method has passed the first lock and is checking for null _searcher = null; } try { _searcher = new IndexSearcher(GetLuceneDirectory(), true); } catch (IOException ex) { throw new ApplicationException("Could not create an index searcher with the supplied lucene directory", ex); } } } } } }