2002-05-10 20:47:25 -04:00
|
|
|
import org.apache.log4j.Category;
|
|
|
|
import org.apache.lucene.document.Document;
|
|
|
|
import org.apache.lucene.search.Hits;
|
2002-05-11 03:01:10 -04:00
|
|
|
import search.SearchResultFactory;
|
2002-05-10 20:47:25 -04:00
|
|
|
|
|
|
|
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>
|
|
|
|
* <p>
|
|
|
|
* <b>Note that this implementation uses code from
|
|
|
|
* /projects/appex/search.</b>
|
|
|
|
* </p>
|
|
|
|
*/
|
|
|
|
public class SearchResults
|
|
|
|
{
|
|
|
|
private static Category cat = Category.getInstance(SearchResults.class);
|
2002-05-11 03:01:10 -04:00
|
|
|
private Document[] hitsDocuments;
|
|
|
|
private Object[] objectResults;
|
2002-05-10 20:47:25 -04:00
|
|
|
private int totalNumberOfResults;
|
|
|
|
|
|
|
|
public SearchResults(Hits hits) throws IOException
|
|
|
|
{
|
|
|
|
this(hits, 0, hits.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
public SearchResults(Hits hits, int from, int to) throws IOException
|
|
|
|
{
|
2002-05-11 03:01:10 -04:00
|
|
|
hitsDocuments = new Document[hits.length()];
|
2002-05-10 20:47:25 -04:00
|
|
|
totalNumberOfResults = hits.length();
|
|
|
|
if (to > totalNumberOfResults)
|
|
|
|
{
|
|
|
|
to = totalNumberOfResults;
|
|
|
|
}
|
|
|
|
for (int i = from; i < to; i++)
|
|
|
|
{
|
2002-05-13 06:56:54 -04:00
|
|
|
hitsDocuments[i] = hits.doc(i);
|
2002-05-10 20:47:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getTotalNumberOfResults()
|
|
|
|
{
|
|
|
|
return totalNumberOfResults;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain the results of the search as objects.
|
|
|
|
*/
|
2002-05-11 03:01:10 -04:00
|
|
|
public Object[] getResultsAsObjects()
|
2002-05-10 20:47:25 -04:00
|
|
|
{
|
|
|
|
if (objectResults == null)
|
|
|
|
{
|
2002-05-11 03:01:10 -04:00
|
|
|
objectResults = new Object[hitsDocuments.length];
|
|
|
|
for (int i = 0; i < hitsDocuments.length; i++)
|
2002-05-10 20:47:25 -04:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2002-05-11 03:01:10 -04:00
|
|
|
objectResults[i] = SearchResultFactory.
|
|
|
|
getDocAsObject(hitsDocuments[i]);
|
2002-05-10 20:47:25 -04:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
cat.error("Error instantiating an object from a document.", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return objectResults;
|
|
|
|
}
|
|
|
|
}
|