Class AbstractAggregator
- java.lang.Object
-
- it.unimi.di.big.mg4j.search.score.AbstractAggregator
-
- All Implemented Interfaces:
Scorer,FlyweightPrototype<Scorer>
- Direct Known Subclasses:
LinearAggregator
public abstract class AbstractAggregator extends Object implements Scorer
AScorerthat aggregates a number of underlyingdelegating scorers, providing equalisation if required.An aggregator combines the results of several scorers following some policy (see, e.g.,
LinearAggregator). In doing so, often the aggregator needs to explore the first scores returned by each scorer, and tune some internal parameters. This procedure, equalisation, is supported by this class: ifequalize(int)is provided with a positive number of samples, they will be fetched from the underlying document iterator, scored, and passed to the implementing subclass so that equalisation information can be properly set up.Additionally, this class ensures that if several scorers need access to intervals, the document iterator to be scored is decorated with a
CachingDocumentIterator, so that several scorer can access intervals.Since this class uses the same document iterator for all aggregated scorers, they must be necessarily delegating scorers.
Implementing subclasses must provide the following methods:
setupEqualizationFactors(), which is called in case equalisation is required and must examineactualSampleselements fromsampleScore(each element is a tuple of scores, one for each scorer) and use that information to set the equalisation factors (ifsamplesis zero, default values must be applied);score(double[]), which must compute the equalised aggregated score using the given array of scores (each to be thought as a score coming from the respective scorer).
Additionally, implementing subclasses must remember to call
equalize(int)when generating a flyweight copy, so that the state of the aggregator is reproduced correctly.
-
-
Field Summary
Fields Modifier and Type Field Description protected intactualSamplesThe actual number of samples obtained (might be less thansamplesif we exhausted the document iterator).protected intcurrSampleThe next sample to be returned, if smaller thanactualSamples.protected double[]currScoreThe current score.protected DocumentIteratordocumentIteratorThe current document iterator.protected intnThe number of underlying scorers.protected booleanneedsCachingWhether we need caching the intervals.protected long[]sampleDocumentCached sample of document pointers.protected intsamplesThe number of samples for equalisation (0 means no equalisation).protected double[][]sampleScoreCached sample of document scores.protected Scorer[]scorerThe underlying scorers.
-
Constructor Summary
Constructors Constructor Description AbstractAggregator(Scorer[] scorer)Creates an aggregator.
-
Method Summary
Modifier and Type Method Description voidequalize(int samples)Set the number of samples for equalisation.Reference2DoubleMap<Index>getWeights()Delegates to the underlying scorers.longnextDocument()Returns the next document provided by this scorer, or -1 if no more documents are available.doublescore()Returns a score for the current document of the last document iterator given toScorer.wrap(DocumentIterator).protected abstract doublescore(double[] score)Computes an aggregated score using the given array of basic scores.doublescore(Index index)Returns a score for the current document of the last document iterator given toScorer.wrap(DocumentIterator), but considering only a given index (optional operation).protected abstract voidsetupEqualizationFactors()Sets up the equalisation factors.booleansetWeights(Reference2DoubleMap<Index> index2weight)Delegates to the underlying scorers.booleanusesIntervals()Delegates to the underlying scorers.voidwrap(DocumentIterator documentIterator)Delegates to the underlying scorers, possibly wrapping the argument in aCachingDocumentIterator; then, ifsamplesis nonzero computes that many document scores and invokessetupEqualizationFactors().
-
-
-
Field Detail
-
documentIterator
protected DocumentIterator documentIterator
The current document iterator.
-
n
protected final int n
The number of underlying scorers.
-
scorer
protected final Scorer[] scorer
The underlying scorers.
-
currScore
protected final double[] currScore
The current score.
-
needsCaching
protected final boolean needsCaching
Whether we need caching the intervals.
-
sampleDocument
protected long[] sampleDocument
Cached sample of document pointers.
-
sampleScore
protected double[][] sampleScore
Cached sample of document scores.
-
samples
protected int samples
The number of samples for equalisation (0 means no equalisation).
-
currSample
protected int currSample
The next sample to be returned, if smaller thanactualSamples.
-
actualSamples
protected int actualSamples
The actual number of samples obtained (might be less thansamplesif we exhausted the document iterator).
-
-
Constructor Detail
-
AbstractAggregator
public AbstractAggregator(Scorer[] scorer)
Creates an aggregator.- Parameters:
scorer- the scorers.
-
-
Method Detail
-
score
public double score(Index index)
Description copied from interface:ScorerReturns a score for the current document of the last document iterator given toScorer.wrap(DocumentIterator), but considering only a given index (optional operation).
-
score
public double score() throws IOExceptionDescription copied from interface:ScorerReturns a score for the current document of the last document iterator given toScorer.wrap(DocumentIterator).- Specified by:
scorein interfaceScorer- Returns:
- the score.
- Throws:
IOException
-
equalize
public void equalize(int samples)
Set the number of samples for equalisation.- Parameters:
samples- the number of samples to be used to equalise scores; a value of zero disables equalisation.- See Also:
AbstractAggregator
-
setWeights
public boolean setWeights(Reference2DoubleMap<Index> index2weight)
Delegates to the underlying scorers.- Specified by:
setWeightsin interfaceScorer- Parameters:
index2weight- a map from indices to weights.- Returns:
- true if at least one underlying scorer supports weights.
-
getWeights
public Reference2DoubleMap<Index> getWeights()
Delegates to the underlying scorers.- Specified by:
getWeightsin interfaceScorer- Returns:
- true if at least one underlying scorer supports weights.
-
usesIntervals
public boolean usesIntervals()
Delegates to the underlying scorers.- Specified by:
usesIntervalsin interfaceScorer- Returns:
- true if at least one underlying scorer uses intervals.
-
wrap
public void wrap(DocumentIterator documentIterator) throws IOException
Delegates to the underlying scorers, possibly wrapping the argument in aCachingDocumentIterator; then, ifsamplesis nonzero computes that many document scores and invokessetupEqualizationFactors().- Specified by:
wrapin interfaceScorer- Parameters:
documentIterator- the document iterator that will be used in subsequent calls toScorer.score()andScorer.score(Index).- Throws:
IOException
-
score
protected abstract double score(double[] score)
Computes an aggregated score using the given array of basic scores. The array is parallel toscorer.- Parameters:
score- an array of scores.- Returns:
- the aggregated scorer.
-
setupEqualizationFactors
protected abstract void setupEqualizationFactors()
Sets up the equalisation factors.Implementations should look into
sampleScoreand set up the equalisation logic. Note that this method is responsible for setting up appropriate equalisation factors even if no equalisation is required (e.g., setting all factors to 1 ).
-
nextDocument
public long nextDocument() throws IOExceptionDescription copied from interface:ScorerReturns the next document provided by this scorer, or -1 if no more documents are available.- Specified by:
nextDocumentin interfaceScorer- Returns:
- the next document, or -1 if no more documents are available.
- Throws:
IOException
-
-