FileDocCategorySizeDatePackage
Hits.javaAPI DocApache Lucene 2.2.06220Sat Jun 16 22:20:34 BST 2007org.apache.lucene.search

Hits

public final class Hits extends Object
A ranked list of documents, used to hold search results.

Caution: Iterate only over the hits needed. Iterating over all hits is generally not desirable and may be the source of performance issues. If you need to iterate over many or all hits, consider using the search method that takes a {@link HitCollector}.

Fields Summary
private Weight
weight
private Searcher
searcher
private Filter
filter
private Sort
sort
private int
length
private Vector
hitDocs
private HitDoc
first
private HitDoc
last
private int
numDocs
private int
maxDocs
Constructors Summary
Hits(Searcher s, Query q, Filter f)

    // max to cache

          
    weight = q.weight(s);
    searcher = s;
    filter = f;
    getMoreDocs(50); // retrieve 100 initially
  
Hits(Searcher s, Query q, Filter f, Sort o)

    weight = q.weight(s);
    searcher = s;
    filter = f;
    sort = o;
    getMoreDocs(50); // retrieve 100 initially
  
Methods Summary
private final voidaddToFront(org.apache.lucene.search.HitDoc hitDoc)

  // insert at front of cache
    if (first == null) {
      last = hitDoc;
    } else {
      first.prev = hitDoc;
    }

    hitDoc.next = first;
    first = hitDoc;
    hitDoc.prev = null;

    numDocs++;
  
public final org.apache.lucene.document.Documentdoc(int n)
Returns the stored fields of the nth document in this set.

Documents are cached, so that repeated requests for the same element may return the same Document object.

throws
CorruptIndexException if the index is corrupt
throws
IOException if there is a low-level IO error

    HitDoc hitDoc = hitDoc(n);

    // Update LRU cache of documents
    remove(hitDoc);               // remove from list, if there
    addToFront(hitDoc);           // add to front of list
    if (numDocs > maxDocs) {      // if cache is full
      HitDoc oldLast = last;
      remove(last);             // flush last
      oldLast.doc = null;       // let doc get gc'd
    }

    if (hitDoc.doc == null) {
      hitDoc.doc = searcher.doc(hitDoc.id);  // cache miss: read document
    }

    return hitDoc.doc;
  
private final voidgetMoreDocs(int min)
Tries to add new documents to hitDocs. Ensures that the hit numbered min has been retrieved.

    if (hitDocs.size() > min) {
      min = hitDocs.size();
    }

    int n = min * 2;	// double # retrieved
    TopDocs topDocs = (sort == null) ? searcher.search(weight, filter, n) : searcher.search(weight, filter, n, sort);
    length = topDocs.totalHits;
    ScoreDoc[] scoreDocs = topDocs.scoreDocs;

    float scoreNorm = 1.0f;
    
    if (length > 0 && topDocs.getMaxScore() > 1.0f) {
      scoreNorm = 1.0f / topDocs.getMaxScore();
    }

    int end = scoreDocs.length < length ? scoreDocs.length : length;
    for (int i = hitDocs.size(); i < end; i++) {
      hitDocs.addElement(new HitDoc(scoreDocs[i].score * scoreNorm,
                                    scoreDocs[i].doc));
    }
  
private final org.apache.lucene.search.HitDochitDoc(int n)

    if (n >= length) {
      throw new IndexOutOfBoundsException("Not a valid hit number: " + n);
    }

    if (n >= hitDocs.size()) {
      getMoreDocs(n);
    }

    return (HitDoc) hitDocs.elementAt(n);
  
public final intid(int n)
Returns the id for the nth document in this set. Note that ids may change when the index changes, so you cannot rely on the id to be stable.

    return hitDoc(n).id;
  
public java.util.Iteratoriterator()
Returns a {@link HitIterator} to navigate the Hits. Each item returned from {@link Iterator#next()} is a {@link Hit}.

Caution: Iterate only over the hits needed. Iterating over all hits is generally not desirable and may be the source of performance issues. If you need to iterate over many or all hits, consider using a search method that takes a {@link HitCollector}.

    return new HitIterator(this);
  
public final intlength()
Returns the total number of hits available in this set.

    return length;
  
private final voidremove(org.apache.lucene.search.HitDoc hitDoc)

	  // remove from cache
    if (hitDoc.doc == null) {     // it's not in the list
      return;					  // abort
    }

    if (hitDoc.next == null) {
      last = hitDoc.prev;
    } else {
      hitDoc.next.prev = hitDoc.prev;
    }

    if (hitDoc.prev == null) {
      first = hitDoc.next;
    } else {
      hitDoc.prev.next = hitDoc.next;
    }

    numDocs--;
  
public final floatscore(int n)
Returns the score for the nth document in this set.

    return hitDoc(n).score;