Mailing List Archive

cvs commit: jakarta-lucene-sandbox/contributions/fulcrum SearchResults.java
kelvint 02/05/10 17:47:25

Added: contributions/fulcrum SearchResults.java
Log:
An object to encapsulate the results of a search.

Revision Changes Path
1.1 jakarta-lucene-sandbox/contributions/fulcrum/SearchResults.java

Index: SearchResults.java
===================================================================
import org.apache.log4j.Category;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.Hits;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import search.SearchResultFactory;

/**
* <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);
private List hitsDocuments;
private List objectResults;
private int totalNumberOfResults;

public SearchResults(Hits hits) throws IOException
{
this(hits, 0, hits.length());
}

public SearchResults(Hits hits, int from, int to) throws IOException
{
hitsDocuments = new ArrayList();
totalNumberOfResults = hits.length();
if (to > totalNumberOfResults)
{
to = totalNumberOfResults;
}
for (int i = from; i < to; i++)
{
hitsDocuments.add(hits.doc(i));
}
}

public int getTotalNumberOfResults()
{
return totalNumberOfResults;
}

/**
* Obtain the results of the search as objects.
*/
public List getResultsAsObjects()
{
if (objectResults == null)
{
objectResults = new ArrayList();
for (Iterator it = hitsDocuments.iterator(); it.hasNext();)
{
try
{
Object o = SearchResultFactory.
getDocAsObject((Document) it.next());
if (!objectResults.contains(o))
objectResults.add(o);
}
catch (Exception e)
{
cat.error("Error instantiating an object from a document.", e);
}
}
}
return objectResults;
}
}




--
To unsubscribe, e-mail: <mailto:lucene-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:lucene-dev-help@jakarta.apache.org>
cvs commit: jakarta-lucene-sandbox/contributions/fulcrum SearchResults.java [ In reply to ]
kelvint 02/05/11 00:01:10

Modified: contributions/fulcrum SearchResults.java
Log:
Refactored to use typed arrays instead of generic lists. (thanks to Otis)
However, the array of objects returned are no longer guaranteed to be unique.
Clients should construct a Set out of the objectResults if uniqueness is required.

Revision Changes Path
1.2 +10 -16 jakarta-lucene-sandbox/contributions/fulcrum/SearchResults.java

Index: SearchResults.java
===================================================================
RCS file: /home/cvs/jakarta-lucene-sandbox/contributions/fulcrum/SearchResults.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- SearchResults.java 11 May 2002 00:47:25 -0000 1.1
+++ SearchResults.java 11 May 2002 07:01:10 -0000 1.2
@@ -1,13 +1,9 @@
import org.apache.log4j.Category;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.Hits;
+import search.SearchResultFactory;

import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-import search.SearchResultFactory;

/**
* <p>
@@ -27,8 +23,8 @@
public class SearchResults
{
private static Category cat = Category.getInstance(SearchResults.class);
- private List hitsDocuments;
- private List objectResults;
+ private Document[] hitsDocuments;
+ private Object[] objectResults;
private int totalNumberOfResults;

public SearchResults(Hits hits) throws IOException
@@ -38,7 +34,7 @@

public SearchResults(Hits hits, int from, int to) throws IOException
{
- hitsDocuments = new ArrayList();
+ hitsDocuments = new Document[hits.length()];
totalNumberOfResults = hits.length();
if (to > totalNumberOfResults)
{
@@ -46,7 +42,7 @@
}
for (int i = from; i < to; i++)
{
- hitsDocuments.add(hits.doc(i));
+ hitsDocuments[i] = hits.doc(i));
}
}

@@ -58,19 +54,17 @@
/**
* Obtain the results of the search as objects.
*/
- public List getResultsAsObjects()
+ public Object[] getResultsAsObjects()
{
if (objectResults == null)
{
- objectResults = new ArrayList();
- for (Iterator it = hitsDocuments.iterator(); it.hasNext();)
+ objectResults = new Object[hitsDocuments.length];
+ for (int i = 0; i < hitsDocuments.length; i++)
{
try
{
- Object o = SearchResultFactory.
- getDocAsObject((Document) it.next());
- if (!objectResults.contains(o))
- objectResults.add(o);
+ objectResults[i] = SearchResultFactory.
+ getDocAsObject(hitsDocuments[i]);
}
catch (Exception e)
{




--
To unsubscribe, e-mail: <mailto:lucene-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:lucene-dev-help@jakarta.apache.org>
cvs commit: jakarta-lucene-sandbox/contributions/fulcrum SearchResults.java [ In reply to ]
kelvint 02/05/13 03:56:54

Modified: contributions/fulcrum SearchResults.java
Log:
Fixed typo.

Revision Changes Path
1.3 +1 -1 jakarta-lucene-sandbox/contributions/fulcrum/SearchResults.java

Index: SearchResults.java
===================================================================
RCS file: /home/cvs/jakarta-lucene-sandbox/contributions/fulcrum/SearchResults.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SearchResults.java 11 May 2002 07:01:10 -0000 1.2
+++ SearchResults.java 13 May 2002 10:56:54 -0000 1.3
@@ -42,7 +42,7 @@
}
for (int i = from; i < to; i++)
{
- hitsDocuments[i] = hits.doc(i));
+ hitsDocuments[i] = hits.doc(i);
}
}





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