FileDocCategorySizeDatePackage
IndexSearcher.javaAPI DocApache Lucene 1.94562Mon Feb 20 09:20:04 GMT 2006org.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. For performance reasons it is recommended to open only one IndexSearcher and use it for all of your searches.

Note that you can only access Hits from an IndexSearcher as long as it is not yet closed, otherwise an IOException will be thrown.

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.Weight weight, int doc)

    return weight.explain(reader, doc);
  
public org.apache.lucene.index.IndexReadergetIndexReader()
Return the {@link IndexReader} this searches.

    return reader;
  
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.TopDocssearch(org.apache.lucene.search.Weight weight, org.apache.lucene.search.Filter filter, int nDocs)


    if (nDocs <= 0)  // null might be returned from hq.top() below.
      throw new IllegalArgumentException("nDocs must be > 0");

    TopDocCollector collector = new TopDocCollector(nDocs);
    search(weight, filter, collector);
    return collector.topDocs();
  
public org.apache.lucene.search.TopFieldDocssearch(org.apache.lucene.search.Weight weight, org.apache.lucene.search.Filter filter, int nDocs, org.apache.lucene.search.Sort sort)


    TopFieldDocCollector collector =
      new TopFieldDocCollector(reader, sort, nDocs);
    search(weight, filter, collector);
    return (TopFieldDocs)collector.topDocs();
  
public voidsearch(org.apache.lucene.search.Weight weight, 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 = weight.scorer(reader);
    if (scorer == null)
      return;
    scorer.score(collector);