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
AScorer
that 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 examineactualSamples
elements fromsampleScore
(each element is a tuple of scores, one for each scorer) and use that information to set the equalisation factors (ifsamples
is 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 int
actualSamples
The actual number of samples obtained (might be less thansamples
if we exhausted the document iterator).protected int
currSample
The next sample to be returned, if smaller thanactualSamples
.protected double[]
currScore
The current score.protected DocumentIterator
documentIterator
The current document iterator.protected int
n
The number of underlying scorers.protected boolean
needsCaching
Whether we need caching the intervals.protected long[]
sampleDocument
Cached sample of document pointers.protected int
samples
The number of samples for equalisation (0 means no equalisation).protected double[][]
sampleScore
Cached sample of document scores.protected Scorer[]
scorer
The underlying scorers.
-
Constructor Summary
Constructors Constructor Description AbstractAggregator(Scorer[] scorer)
Creates an aggregator.
-
Method Summary
Modifier and Type Method Description void
equalize(int samples)
Set the number of samples for equalisation.Reference2DoubleMap<Index>
getWeights()
Delegates to the underlying scorers.long
nextDocument()
Returns the next document provided by this scorer, or -1 if no more documents are available.double
score()
Returns a score for the current document of the last document iterator given toScorer.wrap(DocumentIterator)
.protected abstract double
score(double[] score)
Computes an aggregated score using the given array of basic scores.double
score(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 void
setupEqualizationFactors()
Sets up the equalisation factors.boolean
setWeights(Reference2DoubleMap<Index> index2weight)
Delegates to the underlying scorers.boolean
usesIntervals()
Delegates to the underlying scorers.void
wrap(DocumentIterator documentIterator)
Delegates to the underlying scorers, possibly wrapping the argument in aCachingDocumentIterator
; then, ifsamples
is 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 thansamples
if 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:Scorer
Returns 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 IOException
Description copied from interface:Scorer
Returns a score for the current document of the last document iterator given toScorer.wrap(DocumentIterator)
.- Specified by:
score
in 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:
setWeights
in 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:
getWeights
in interfaceScorer
- Returns:
- true if at least one underlying scorer supports weights.
-
usesIntervals
public boolean usesIntervals()
Delegates to the underlying scorers.- Specified by:
usesIntervals
in 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, ifsamples
is nonzero computes that many document scores and invokessetupEqualizationFactors()
.- Specified by:
wrap
in 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
sampleScore
and 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 IOException
Description copied from interface:Scorer
Returns the next document provided by this scorer, or -1 if no more documents are available.- Specified by:
nextDocument
in interfaceScorer
- Returns:
- the next document, or -1 if no more documents are available.
- Throws:
IOException
-
-