org.apache.lucene.search
Class ScoreCachingWrappingScorer

java.lang.Object
  extended by org.apache.lucene.search.DocIdSetIterator
      extended by org.apache.lucene.search.Scorer
          extended by org.apache.lucene.search.ScoreCachingWrappingScorer

public class ScoreCachingWrappingScorer
extends Scorer

A Scorer which wraps another scorer and caches the score of the current document. Successive calls to score() will return the same result and will not invoke the wrapped Scorer's score() method, unless the current document has changed.
This class might be useful due to the changes done to the Collector interface, in which the score is not computed for a document by default, only if the collector requests it. Some collectors may need to use the score in several places, however all they have in hand is a Scorer object, and might end up computing the score of a document more than once.


Nested Class Summary
 
Nested classes/interfaces inherited from class org.apache.lucene.search.Scorer
Scorer.ScorerVisitor<P extends Query,C extends Query,S extends Scorer>
 
Field Summary
 
Fields inherited from class org.apache.lucene.search.Scorer
weight
 
Fields inherited from class org.apache.lucene.search.DocIdSetIterator
NO_MORE_DOCS
 
Constructor Summary
ScoreCachingWrappingScorer(Scorer scorer)
          Creates a new instance by wrapping the given scorer.
 
Method Summary
 int advance(int target)
          Advances to the first beyond (see NOTE below) the current whose document number is greater than or equal to target.
 int docID()
          Returns the following: -1 or DocIdSetIterator.NO_MORE_DOCS if DocIdSetIterator.nextDoc() or DocIdSetIterator.advance(int) were not called yet.
 Similarity getSimilarity()
          Returns the Similarity implementation used by this scorer.
 int nextDoc()
          Advances to the next document in the set and returns the doc it is currently on, or DocIdSetIterator.NO_MORE_DOCS if there are no more docs in the set.
NOTE: after the iterator has exhausted you should not call this method, as it may result in unpredicted behavior.
 float score()
          Returns the score of the current document matching the query.
 void score(Collector collector)
          Scores and collects all matching documents.
protected  boolean score(Collector collector, int max, int firstDocID)
          Expert: Collects matching documents in a range.
 
Methods inherited from class org.apache.lucene.search.Scorer
freq, visitScorers, visitSubScorers
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ScoreCachingWrappingScorer

public ScoreCachingWrappingScorer(Scorer scorer)
Creates a new instance by wrapping the given scorer.

Method Detail

score

protected boolean score(Collector collector,
                        int max,
                        int firstDocID)
                 throws IOException
Description copied from class: Scorer
Expert: Collects matching documents in a range. Hook for optimization. Note, firstDocID is added to ensure that DocIdSetIterator.nextDoc() was called before this method.

NOTE: Because of backwards compatibility, this method is still declared as protected, but it is intended to be public, because it's called from other classes (like BooleanScorer). If you subclass Scorer, you should declare the overridden method as public to ease transition to Lucene 4.0, where it will be public.

Overrides:
score in class Scorer
Parameters:
collector - The collector to which all matching documents are passed.
max - Do not score documents past this.
firstDocID - The first document ID (ensures DocIdSetIterator.nextDoc() is called before this method.
Returns:
true if more matching documents may remain.
Throws:
IOException

getSimilarity

public Similarity getSimilarity()
Description copied from class: Scorer
Returns the Similarity implementation used by this scorer.

Overrides:
getSimilarity in class Scorer

score

public float score()
            throws IOException
Description copied from class: Scorer
Returns the score of the current document matching the query. Initially invalid, until DocIdSetIterator.nextDoc() or DocIdSetIterator.advance(int) is called the first time, or when called from within Collector.collect(int).

Specified by:
score in class Scorer
Throws:
IOException

docID

public int docID()
Description copied from class: DocIdSetIterator
Returns the following:

Specified by:
docID in class DocIdSetIterator

nextDoc

public int nextDoc()
            throws IOException
Description copied from class: DocIdSetIterator
Advances to the next document in the set and returns the doc it is currently on, or DocIdSetIterator.NO_MORE_DOCS if there are no more docs in the set.
NOTE: after the iterator has exhausted you should not call this method, as it may result in unpredicted behavior.

Specified by:
nextDoc in class DocIdSetIterator
Throws:
IOException

score

public void score(Collector collector)
           throws IOException
Description copied from class: Scorer
Scores and collects all matching documents.

Overrides:
score in class Scorer
Parameters:
collector - The collector to which all matching documents are passed.
Throws:
IOException

advance

public int advance(int target)
            throws IOException
Description copied from class: DocIdSetIterator
Advances to the first beyond (see NOTE below) the current whose document number is greater than or equal to target. Returns the current document number or DocIdSetIterator.NO_MORE_DOCS if there are no more docs in the set.

Behaves as if written:

 int advance(int target) {
   int doc;
   while ((doc = nextDoc()) < target) {
   }
   return doc;
 }
 
Some implementations are considerably more efficient than that.

NOTE: when target ≤ current implementations may opt not to advance beyond their current DocIdSetIterator.docID().

NOTE: this method may be called with DocIdSetIterator.NO_MORE_DOCS for efficiency by some Scorers. If your implementation cannot efficiently determine that it should exhaust, it is recommended that you check for that value in each call to this method.

NOTE: after the iterator has exhausted you should not call this method, as it may result in unpredicted behavior.

Specified by:
advance in class DocIdSetIterator
Throws:
IOException