SearchService is no longer supported or actively developed. Perhaps the code will live on, but as a subsystem instead.

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150883 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Kelvin Tan 2003-05-02 01:36:53 +00:00
parent b7db8168cc
commit 48ea78f45a
3 changed files with 0 additions and 300 deletions

View File

@ -1,139 +0,0 @@
import org.apache.fulcrum.InitializationException;
import org.apache.fulcrum.ServiceException;
import org.apache.log4j.Category;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.search.*;
import java.io.IOException;
/**
* Implementation of {@link SearchService}.
*/
public class LuceneSearchService
extends org.apache.fulcrum.BaseService implements SearchService
{
/**
* Log4j category.
*/
private static Category cat = Category.getInstance(LuceneSearchService.class);
/**
* The analyzer used for searching and indexing. Analyzers have no
* state so its ok to return the same analyzer to clients.
*/
private Analyzer analyzer;
/**
* Is the index locked?
*/
private boolean indexLocked = false;
/**
* Filesystem location of the index.
*/
private String searchIndexLocation;
public void init() throws InitializationException
{
searchIndexLocation = getConfiguration().getString(SearchService.INDEX_LOCATION_KEY);
setInit(true);
}
public SearchResults search(Query query) throws ServiceException
{
return search(query, null);
}
public SearchResults search(Query query, Filter filter) throws ServiceException
{
Searcher searcher = null;
SearchResults results = null;
try
{
searcher = new IndexSearcher(searchIndexLocation);
Hits hits = searcher.search(query, filter);
results = new SearchResults(hits);
}
catch (IOException ioe)
{
throw new ServiceException("Error encountered searching!", ioe);
}
finally
{
try
{
if (searcher != null)
searcher.close();
}
catch (IOException ioe)
{
throw new ServiceException("Error encountered searching!", ioe);
}
return results;
}
}
public SearchResults search(Query query, Filter filter,
int from, int to) throws ServiceException
{
Searcher searcher = null;
SearchResults results = null;
try
{
searcher = new IndexSearcher(searchIndexLocation);
Hits hits = searcher.search(query, filter);
results = new SearchResults(hits, from, to);
}
catch (IOException ioe)
{
throw new ServiceException("Error encountered searching!", ioe);
}
finally
{
try
{
if (searcher != null)
searcher.close();
}
catch (IOException ioe)
{
throw new ServiceException("Error encountered searching!", ioe);
}
return results;
}
}
public void batchIndex() throws ServiceException
{
throw new UnsupportedOperationException();
}
public boolean isIndexing()
{
return indexLocked;
}
public Analyzer getAnalyzer()
{
if (analyzer == null)
{
analyzer = new StandardAnalyzer();
}
return analyzer;
}
protected synchronized void acquireIndexLock() throws InterruptedException
{
while (isIndexing())
{
wait(500);
}
indexLocked = true;
}
protected synchronized void releaseIndexLock()
{
indexLocked = false;
}
}

View File

@ -1,89 +0,0 @@
import org.apache.log4j.Category;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.Hits;
import java.io.IOException;
/**
* <p>
* Encapsulates the results of a search. After a SearchResults has
* been constructed from a Hits object, the IndexSearcher can be
* safely closed.
* </p>
* <p>
* SearchResults also provides a way of retrieving Java objects from
* Documents (via {@link search.SearchResultsFactory}).
* </p>
*/
public class SearchResults
{
private static Category cat = Category.getInstance(SearchResults.class);
private Document[] hitsDocuments;
private Object[] objectResults;
private int totalNumberOfResults;
public SearchResults(Hits hits) throws IOException
{
this(hits, 0, hits.length() - 1);
}
public SearchResults(Hits hits, int from, int to) throws IOException
{
totalNumberOfResults = hits.length();
if (to > totalNumberOfResults)
{
to = totalNumberOfResults - 1;
}
int numberOfResults = to - from + 1;
if (numberOfResults > -1)
{
hitsDocuments = new Document[numberOfResults];
for (int i = to, j = 0; i >= from; i--, j++)
{
hitsDocuments[j] = hits.doc(i);
}
}
else
{
throw new IllegalArgumentException("Range of results requested " +
"exceed total number of search " +
"results returned.");
}
}
public int getTotalNumberOfResults()
{
return totalNumberOfResults;
}
/**
* Obtain the results of the search as objects. The objects returned are
* not guaranteed to be unique.
*/
public Object[] getResultsAsObjects()
{
/**
* At this point, use some mechanism of retrieving
* the objects via a UUID or something.
*/
/*
if (objectResults == null)
{
objectResults = new Object[hitsDocuments.length];
for (int i = 0; i < hitsDocuments.length; i++)
{
try
{
objectResults[i] = SearchResultFactory.
getDocAsObject(hitsDocuments[i]);
}
catch (Exception e)
{
cat.error("Error instantiating an object from a document.", e);
}
}
}
*/
return objectResults;
}
}

View File

@ -1,72 +0,0 @@
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Query;
import org.apache.lucene.document.Document;
import org.apache.fulcrum.ServiceException;
import org.apache.fulcrum.Service;
import java.util.Map;
/**
* A SearchService based on the Fulcrum services framework.
*/
public interface SearchService extends Service
{
/**
* The key in the TurbineResources.properties that references this
* service.
*/
public static final String SERVICE_NAME = "SearchService";
/**
* The key in SearchService properties in
* TurbineResources.properties. The location of the index.
* Assumes a FSDirectory is used.
*/
public static final String INDEX_LOCATION_KEY = "index.location";
/**
* Performs a search.
*
* @param Query to search on.
* @return SearchResults
* @exception ServiceException
*/
public SearchResults search(Query query) throws ServiceException;
/**
* Performs a search, using a filter to filter the results.
*
* @param Query to search on.
* @param Filter to filter the results through.
* @return SearchResults
* @exception ServiceException
*/
public SearchResults search(Query query, Filter filter) throws ServiceException;
/**
* Performs a search, using a filter to filter the results, then
* return the results within the range specified.
*
* @param Query to search on.
* @return SearchResults
* @exception ServiceException
*/
public SearchResults search(Query query, Filter filter,
int from, int to) throws ServiceException;
/**
* Refresh the entire index.
*/
public void batchIndex() throws ServiceException;
/**
* Is the indexer currently indexing?
*/
public boolean isIndexing();
/**
* Get the analyzer used.
*/
public Analyzer getAnalyzer();
}