lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ben Pryor <BPr...@widen.com>
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
class). 

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
session.

package org.apache.lucene.search;

import java.io.IOException;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.store.Directory;

/**
 * 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 = IndexReader.open(directory);
	}
	
	public void close() throws IOException 
	{
		reader.close();
		reader = null;
	}
	
	public boolean isOpen()
	{
		return reader != null;
	}
}

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-dev-help@jakarta.apache.org


Mime
View raw message