FileDocCategorySizeDatePackage
ReqExclScorer.javaAPI DocApache Lucene 2.1.04637Wed Feb 14 10:46:40 GMT 2007org.apache.lucene.search

ReqExclScorer

public class ReqExclScorer extends Scorer
A Scorer for queries with a required subscorer and an excluding (prohibited) subscorer.
This Scorer implements {@link Scorer#skipTo(int)}, and it uses the skipTo() on the given scorers.

Fields Summary
private Scorer
reqScorer
private Scorer
exclScorer
private boolean
firstTime
Constructors Summary
public ReqExclScorer(Scorer reqScorer, Scorer exclScorer)
Construct a ReqExclScorer.

param
reqScorer The scorer that must match, except where
param
exclScorer indicates exclusion.

    super(null); // No similarity used.
    this.reqScorer = reqScorer;
    this.exclScorer = exclScorer;
  
Methods Summary
public intdoc()

    return reqScorer.doc(); // reqScorer may be null when next() or skipTo() already return false
  
public org.apache.lucene.search.Explanationexplain(int doc)

    Explanation res = new Explanation();
    if (exclScorer.skipTo(doc) && (exclScorer.doc() == doc)) {
      res.setDescription("excluded");
    } else {
      res.setDescription("not excluded");
      res.addDetail(reqScorer.explain(doc));
    }
    return res;
  
public booleannext()

  
       
    if (firstTime) {
      if (! exclScorer.next()) {
        exclScorer = null; // exhausted at start
      }
      firstTime = false;
    }
    if (reqScorer == null) {
      return false;
    }
    if (! reqScorer.next()) {
      reqScorer = null; // exhausted, nothing left
      return false;
    }
    if (exclScorer == null) {
      return true; // reqScorer.next() already returned true
    }
    return toNonExcluded();
  
public floatscore()
Returns the score of the current document matching the query. Initially invalid, until {@link #next()} is called the first time.

return
The score of the required scorer.

    return reqScorer.score(); // reqScorer may be null when next() or skipTo() already return false
  
public booleanskipTo(int target)
Skips to the first match beyond the current whose document number is greater than or equal to a given target.
When this method is used the {@link #explain(int)} method should not be used.

param
target The target document number.
return
true iff there is such a match.

    if (firstTime) {
      firstTime = false;
      if (! exclScorer.skipTo(target)) {
        exclScorer = null; // exhausted
      }
    }
    if (reqScorer == null) {
      return false;
    }
    if (exclScorer == null) {
      return reqScorer.skipTo(target);
    }
    if (! reqScorer.skipTo(target)) {
      reqScorer = null;
      return false;
    }
    return toNonExcluded();
  
private booleantoNonExcluded()
Advance to non excluded doc.
On entry:
  • reqScorer != null,
  • exclScorer != null,
  • reqScorer was advanced once via next() or skipTo() and reqScorer.doc() may still be excluded.
Advances reqScorer a non excluded required doc, if any.

return
true iff there is a non excluded required doc.

    int exclDoc = exclScorer.doc();
    do {  
      int reqDoc = reqScorer.doc(); // may be excluded
      if (reqDoc < exclDoc) {
        return true; // reqScorer advanced to before exclScorer, ie. not excluded
      } else if (reqDoc > exclDoc) {
        if (! exclScorer.skipTo(reqDoc)) {
          exclScorer = null; // exhausted, no more exclusions
          return true;
        }
        exclDoc = exclScorer.doc();
        if (exclDoc > reqDoc) {
          return true; // not excluded
        }
      }
    } while (reqScorer.next());
    reqScorer = null; // exhausted, nothing left
    return false;