org.apache.lucene.search
Class DelayCloseIndexSearcher

java.lang.Object
  extended byorg.apache.lucene.search.Searcher
      extended byorg.apache.lucene.search.IndexSearcher
          extended byorg.apache.lucene.search.DelayCloseIndexSearcher
All Implemented Interfaces:
java.rmi.Remote, org.apache.lucene.search.Searchable

public class DelayCloseIndexSearcher
extends org.apache.lucene.search.IndexSearcher

Implements search over a single IndexReader, but remains open even if close() is called. This way it can be shared by multiple objects that need to search the index without being aware of the keep-the-index-open-until-it-changes logic.

Basic use (works, but can slow all searcher while opening a new Searcher instance:

 class SearcherFactory {
     SearcherFactory(Directory directory){
         currentSearcher=new DelayCloseIndexSearcher(directory);
     }
 

public synchronized IndexSearcher createSearcher() { if (!currentSearcher.isCurrent()) { currentSearcher.closeWhenDone(); currentSearcher=new DelayCloseIndexSearcher(directory); }

currentSearcher.open(); return currentSearcher; }

void synchronized close() { currentSearcher.closeWhenDone(); }

private final Directory directory;

private DelayCloseIndexSearcher currentSearcher; }

Objects that need to search the index do the following:

 //searcherFactory is created once at startup
 

IndexSearcher indexSearcher=searcherFactory.createSearcher(); Hits hist=indexSearcher.search(query, filter, sort); ... // handle results indexSearcher.close();

// when the application shuts down searcherFactory.close();

Author:
Luc Vanlerberghe

Constructor Summary
DelayCloseIndexSearcher(org.apache.lucene.store.Directory directory)
          Creates a DelayCloseIndexSearcher searching the index in the provided directory..
DelayCloseIndexSearcher(org.apache.lucene.index.IndexReader indexReader)
          Creates a DelayCloseIndexSearcher searching the index using the provided Reader
DelayCloseIndexSearcher(org.apache.lucene.index.IndexReader indexReader, org.apache.lucene.store.Directory directory)
          Deprecated. Use DelayCloseIndexSearcher(org.apache.lucene.index.IndexReader) instead
DelayCloseIndexSearcher(java.lang.String string)
           
 
Method Summary
 void close()
          Should be called once for every call to open().
 void closeWhenDone()
          Signals that this instance may really close when all open() calls have been balanced with a call to close().
 boolean isClosed()
          Returns wether the underlying IndexSearcher has really been closed.
 boolean isCurrent()
          Returns whether the underlying IndexSearcher instance still works on a current version of the index.
 void open()
          This should be called whenever this instances is passed as a new IndexSearcher.
 
Methods inherited from class org.apache.lucene.search.IndexSearcher
doc, doc, docFreq, explain, getIndexReader, maxDoc, rewrite, search, search, search
 
Methods inherited from class org.apache.lucene.search.Searcher
createWeight, docFreqs, explain, getSimilarity, search, search, search, search, search, search, search, search, setSimilarity
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DelayCloseIndexSearcher

public DelayCloseIndexSearcher(java.lang.String string)
                        throws java.io.IOException

DelayCloseIndexSearcher

public DelayCloseIndexSearcher(org.apache.lucene.store.Directory directory)
                        throws java.io.IOException
Creates a DelayCloseIndexSearcher searching the index in the provided directory..

Parameters:
directory - containing the index.
Throws:
java.io.IOException - if an I/O error occurs.

DelayCloseIndexSearcher

public DelayCloseIndexSearcher(org.apache.lucene.index.IndexReader indexReader)
                        throws java.io.IOException
Creates a DelayCloseIndexSearcher searching the index using the provided Reader

Parameters:
indexReader - Reader to use for opening the searcher
Throws:
java.io.IOException - if an I/O error occurs

DelayCloseIndexSearcher

public DelayCloseIndexSearcher(org.apache.lucene.index.IndexReader indexReader,
                               org.apache.lucene.store.Directory directory)
                        throws java.io.IOException
Deprecated. Use DelayCloseIndexSearcher(org.apache.lucene.index.IndexReader) instead

Parameters:
indexReader - The indexReader to use for the searcher
directory - No longer in use
Throws:
java.io.IOException - If there's a problem opening the underlying searcher
Method Detail

open

public void open()
This should be called whenever this instances is passed as a new IndexSearcher. Only when each call to open() is balanced with a call to close(), and closeWhenDone has been called, will super.close() be called.


closeWhenDone

public void closeWhenDone()
                   throws java.io.IOException
Signals that this instance may really close when all open() calls have been balanced with a call to close().

Throws:
java.io.IOException - if an I/O error occurs.

isCurrent

public boolean isCurrent()
                  throws java.io.IOException
Returns whether the underlying IndexSearcher instance still works on a current version of the index. If it returns false, closeWhenDone() should be called and another instance created to handle further search requests.

Returns:
whether the underlying IndexSearcher instance still works on a current version of the index
Throws:
java.io.IOException - if an I/O error occurs.

isClosed

public boolean isClosed()
Returns wether the underlying IndexSearcher has really been closed. If it is true, this instance can no longer be used.

Returns:
whether the underlying IndexSearcher has really been closed.

close

public void close()
           throws java.io.IOException
Should be called once for every call to open(). If the usageCount drops to zero and closeWhenDone() was called, super.close() will be called.

Throws:
java.io.IOException - if an I/O error occurs.


Copyright © 2006-2009 Atlassian Software Systems Pty Ltd. All Rights Reserved.