FileDocCategorySizeDatePackage
IndexSearcher.javaAPI DocApache Lucene 1.4.35610Thu Oct 07 09:50:22 BST 2004org.apache.lucene.search

IndexSearcher

public class IndexSearcher extends Searcher
Implements search over a single IndexReader.

Applications usually need only call the inherited {@link #search(Query)} or {@link #search(Query,Filter)} methods.

Fields Summary
IndexReader
reader
private boolean
closeReader
Constructors Summary
public IndexSearcher(String path)
Creates a searcher searching the index in the named directory.

    this(IndexReader.open(path), true);
  
public IndexSearcher(Directory directory)
Creates a searcher searching the index in the provided directory.

    this(IndexReader.open(directory), true);
  
public IndexSearcher(IndexReader r)
Creates a searcher searching the provided index.

    this(r, false);
  
private IndexSearcher(IndexReader r, boolean closeReader)

    reader = r;
    this.closeReader = closeReader;
  
Methods Summary
public voidclose()
Note that the underlying IndexReader is not closed, if IndexSearcher was constructed with IndexSearcher(IndexReader r). If the IndexReader was supplied implicitly by specifying a directory, then the IndexReader gets closed.

    if(closeReader)
      reader.close();
  
public org.apache.lucene.document.Documentdoc(int i)

    return reader.document(i);
  
public intdocFreq(org.apache.lucene.index.Term term)

    return reader.docFreq(term);
  
public org.apache.lucene.search.Explanationexplain(org.apache.lucene.search.Query query, int doc)

    return query.weight(this).explain(reader, doc);
  
public intmaxDoc()

    return reader.maxDoc();
  
public org.apache.lucene.search.Queryrewrite(org.apache.lucene.search.Query original)

    Query query = original;
    for (Query rewrittenQuery = query.rewrite(reader); rewrittenQuery != query;
         rewrittenQuery = query.rewrite(reader)) {
      query = rewrittenQuery;
    }
    return query;
  
public org.apache.lucene.search.TopFieldDocssearch(org.apache.lucene.search.Query query, org.apache.lucene.search.Filter filter, int nDocs, org.apache.lucene.search.Sort sort)

    Scorer scorer = query.weight(this).scorer(reader);
    if (scorer == null)
      return new TopFieldDocs(0, new ScoreDoc[0], sort.fields);

    final BitSet bits = filter != null ? filter.bits(reader) : null;
    final FieldSortedHitQueue hq =
      new FieldSortedHitQueue(reader, sort.fields, nDocs);
    final int[] totalHits = new int[1];
    scorer.score(new HitCollector() {
        public final void collect(int doc, float score) {
          if (score > 0.0f &&			  // ignore zeroed buckets
              (bits==null || bits.get(doc))) {	  // skip docs not in bits
            totalHits[0]++;
            hq.insert(new FieldDoc(doc, score));
          }
        }
      });

    ScoreDoc[] scoreDocs = new ScoreDoc[hq.size()];
    for (int i = hq.size()-1; i >= 0; i--)	  // put docs in array
      scoreDocs[i] = hq.fillFields ((FieldDoc) hq.pop());

    return new TopFieldDocs(totalHits[0], scoreDocs, hq.getFields());
  
public voidsearch(org.apache.lucene.search.Query query, org.apache.lucene.search.Filter filter, org.apache.lucene.search.HitCollector results)

    HitCollector collector = results;
    if (filter != null) {
      final BitSet bits = filter.bits(reader);
      collector = new HitCollector() {
	  public final void collect(int doc, float score) {
	    if (bits.get(doc)) {		  // skip docs not in bits
	      results.collect(doc, score);
	    }
	  }
	};
    }

    Scorer scorer = query.weight(this).scorer(reader);
    if (scorer == null)
      return;
    scorer.score(collector);
  
public org.apache.lucene.search.TopDocssearch(org.apache.lucene.search.Query query, org.apache.lucene.search.Filter filter, int nDocs)

    Scorer scorer = query.weight(this).scorer(reader);
    if (scorer == null)
      return new TopDocs(0, new ScoreDoc[0]);

    final BitSet bits = filter != null ? filter.bits(reader) : null;
    final HitQueue hq = new HitQueue(nDocs);
    final int[] totalHits = new int[1];
    scorer.score(new HitCollector() {
        private float minScore = 0.0f;
	public final void collect(int doc, float score) {
	  if (score > 0.0f &&			  // ignore zeroed buckets
	      (bits==null || bits.get(doc))) {	  // skip docs not in bits
	    totalHits[0]++;
            if (hq.size() < nDocs || score >= minScore) {
              hq.insert(new ScoreDoc(doc, score));
              minScore = ((ScoreDoc)hq.top()).score; // maintain minScore
            }
	  }
	}
      });

    ScoreDoc[] scoreDocs = new ScoreDoc[hq.size()];
    for (int i = hq.size()-1; i >= 0; i--)	  // put docs in array
      scoreDocs[i] = (ScoreDoc)hq.pop();

    return new TopDocs(totalHits[0], scoreDocs);