lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ben Pryor <>
Subject Saving a Searcher across requests
Date Wed, 08 Sep 2004 15:16:56 GMT
I wrote a small extension to the IndexSearcher class to allow a Searcher and
any Hits objects created by that Searcher to be saved in between client
requests in a web application. The need was for a way to store and use a
Hits object across multiple client requests without keeping any files open
in between requests. The idea here is that the SaveableIndexSearcher object
and a Hits object get stored in the HttpSession (possibly inside a wrapper

The way it works is pretty simple: the SaveableIndexSearcher just has an
open() and close() method to open and close its internal IndexReader. During
request processing (server-side) the SaveableIndexSearcher is opened, and
any previous Hits objects it created are usable. Before the response is sent
back to the client, the SaveableIndexSearcher is closed, and stored in the



import org.apache.lucene.index.IndexReader;

 * SaveableIndexSearcher - a Searcher that can be saved in between 
 * client requests in a web application,
 * avoiding having to re-run a search for each request
public class SaveableIndexSearcher extends IndexSearcher
	private Directory directory;
	public SaveableIndexSearcher(Directory d) 
		super((IndexReader) null);
		directory = d;
	public void open() throws IOException
		reader =;
	public void close() throws IOException 
		reader = null;
	public boolean isOpen()
		return reader != null;

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message