FileDocCategorySizeDatePackage
MultiSearcher.javaAPI DocApache Lucene 1.4.36155Tue Mar 30 00:48:04 BST 2004org.apache.lucene.search

MultiSearcher

public class MultiSearcher extends Searcher
Implements search over a set of Searchables.

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

Fields Summary
private Searchable[]
searchables
private int[]
starts
private int
maxDoc
Constructors Summary
public MultiSearcher(Searchable[] searchables)
Creates a searcher which searches searchables.


         
       
    this.searchables = searchables;

    starts = new int[searchables.length + 1];	  // build starts array
    for (int i = 0; i < searchables.length; i++) {
      starts[i] = maxDoc;
      maxDoc += searchables[i].maxDoc();          // compute maxDocs
    }
    starts[searchables.length] = maxDoc;
  
Methods Summary
public voidclose()

    for (int i = 0; i < searchables.length; i++)
      searchables[i].close();
  
public org.apache.lucene.document.Documentdoc(int n)

    int i = subSearcher(n);			  // find searcher index
    return searchables[i].doc(n - starts[i]);	  // dispatch to searcher
  
public intdocFreq(org.apache.lucene.index.Term term)

    int docFreq = 0;
    for (int i = 0; i < searchables.length; i++)
      docFreq += searchables[i].docFreq(term);
    return docFreq;
  
public org.apache.lucene.search.Explanationexplain(org.apache.lucene.search.Query query, int doc)

    int i = subSearcher(doc);			  // find searcher index
    return searchables[i].explain(query,doc-starts[i]); // dispatch to searcher
  
protected int[]getStarts()

  	return starts;
  
public intmaxDoc()

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

    Query[] queries = new Query[searchables.length];
    for (int i = 0; i < searchables.length; i++) {
      queries[i] = searchables[i].rewrite(original);
    }
    return original.combine(queries);
  
public org.apache.lucene.search.TopDocssearch(org.apache.lucene.search.Query query, org.apache.lucene.search.Filter filter, int nDocs)

    HitQueue hq = new HitQueue(nDocs);
    int totalHits = 0;

    for (int i = 0; i < searchables.length; i++) { // search each searcher
      TopDocs docs = searchables[i].search(query, filter, nDocs);
      totalHits += docs.totalHits;		  // update totalHits
      ScoreDoc[] scoreDocs = docs.scoreDocs;
      for (int j = 0; j < scoreDocs.length; j++) { // merge scoreDocs into hq
	ScoreDoc scoreDoc = scoreDocs[j];
        scoreDoc.doc += starts[i];                // convert doc
        if(!hq.insert(scoreDoc))
            break;                                // no more scores > 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, scoreDocs);
  
public org.apache.lucene.search.TopFieldDocssearch(org.apache.lucene.search.Query query, org.apache.lucene.search.Filter filter, int n, org.apache.lucene.search.Sort sort)

    FieldDocSortedHitQueue hq = null;
    int totalHits = 0;

    for (int i = 0; i < searchables.length; i++) { // search each searcher
      TopFieldDocs docs = searchables[i].search (query, filter, n, sort);
      if (hq == null) hq = new FieldDocSortedHitQueue (docs.fields, n);
      totalHits += docs.totalHits;		  // update totalHits
      ScoreDoc[] scoreDocs = docs.scoreDocs;
      for (int j = 0; j < scoreDocs.length; j++) { // merge scoreDocs into hq
        ScoreDoc scoreDoc = scoreDocs[j];
        scoreDoc.doc += starts[i];                // convert doc
        if (!hq.insert (scoreDoc))
          break;                                  // no more scores > 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 TopFieldDocs (totalHits, scoreDocs, hq.getFields());
  
public voidsearch(org.apache.lucene.search.Query query, org.apache.lucene.search.Filter filter, org.apache.lucene.search.HitCollector results)

    for (int i = 0; i < searchables.length; i++) {

      final int start = starts[i];

      searchables[i].search(query, filter, new HitCollector() {
	  public void collect(int doc, float score) {
	    results.collect(doc + start, score);
	  }
	});

    }
  
public intsearcherIndex(int n)
Call {@link #subSearcher} instead.

deprecated

    return subSearcher(n);
  
public intsubDoc(int n)
Returns the document number of document n within its sub-index.

    return n - starts[subSearcher(n)];
  
public intsubSearcher(int n)
Returns index of the searcher for document n in the array used to construct this searcher.

                 // find searcher for doc n:
    // replace w/ call to Arrays.binarySearch in Java 1.2
    int lo = 0;					  // search starts array
    int hi = searchables.length - 1;		  // for first element less
						  // than n, return its index
    while (hi >= lo) {
      int mid = (lo + hi) >> 1;
      int midValue = starts[mid];
      if (n < midValue)
	hi = mid - 1;
      else if (n > midValue)
	lo = mid + 1;
      else {                                      // found a match
        while (mid+1 < searchables.length && starts[mid+1] == midValue) {
          mid++;                                  // scan to last match
        }
	return mid;
      }
    }
    return hi;