SOLR-14378: Factor a FilterFeatureScorer class out from (contrib/ltr) OriginalScoreScorer.

This commit is contained in:
Christine Poerschke 2020-04-03 11:19:03 +01:00
parent 28dea8d327
commit ac2837cfbd
3 changed files with 55 additions and 30 deletions

View File

@ -27,6 +27,7 @@ import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.TwoPhaseIterator;
import org.apache.lucene.search.Weight;
import org.apache.lucene.util.Accountable;
import org.apache.lucene.util.RamUsageEstimator;
@ -340,6 +341,46 @@ public abstract class Feature extends Query implements Accountable {
}
}
/**
* A <code>FeatureScorer</code> that contains a <code>Scorer</code>,
* which it delegates to where appropriate.
*/
public abstract class FilterFeatureScorer extends FeatureScorer {
final protected Scorer in;
public FilterFeatureScorer(Feature.FeatureWeight weight, Scorer scorer) {
super(weight, null);
this.in = scorer;
}
@Override
public int docID() {
return in.docID();
}
@Override
public DocIdSetIterator iterator() {
return in.iterator();
}
@Override
public TwoPhaseIterator twoPhaseIterator() {
return in.twoPhaseIterator();
}
@Override
public int advanceShallow(int target) throws IOException {
return in.advanceShallow(target);
}
@Override
public float getMaxScore(int upTo) throws IOException {
return in.getMaxScore(upTo);
}
}
/**
* Default FeatureScorer class that returns the score passed in. Can be used
* as a simple ValueFeature, or to return a default scorer in case an

View File

@ -21,7 +21,6 @@ import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreMode;
@ -84,12 +83,10 @@ public class OriginalScoreFeature extends Feature {
return new OriginalScoreScorer(this, originalScorer);
}
public class OriginalScoreScorer extends FeatureScorer {
final private Scorer originalScorer;
public class OriginalScoreScorer extends FilterFeatureScorer {
public OriginalScoreScorer(FeatureWeight weight, Scorer originalScorer) {
super(weight,null);
this.originalScorer = originalScorer;
super(weight, originalScorer);
}
@Override
@ -98,23 +95,9 @@ public class OriginalScoreFeature extends Feature {
// was already scored in step 1
// we shouldn't need to calc original score again.
final DocInfo docInfo = getDocInfo();
return (docInfo != null && docInfo.hasOriginalDocScore() ? docInfo.getOriginalDocScore() : originalScorer.score());
return (docInfo != null && docInfo.hasOriginalDocScore() ? docInfo.getOriginalDocScore() : in.score());
}
@Override
public float getMaxScore(int upTo) throws IOException {
return Float.POSITIVE_INFINITY;
}
@Override
public int docID() {
return originalScorer.docID();
}
@Override
public DocIdSetIterator iterator() {
return originalScorer.iterator();
}
}
}

View File

@ -17,31 +17,32 @@
package org.apache.solr.ltr.feature;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import org.apache.lucene.search.Scorer;
import org.apache.solr.SolrTestCase;
import org.junit.Test;
public class TestOriginalScoreScorer extends SolrTestCase {
public class TestFeature extends SolrTestCase {
@Test
public void testOverridesAbstractScorerMethods() {
final Class<?> ossClass = OriginalScoreFeature.OriginalScoreWeight.OriginalScoreScorer.class;
public void testFilterFeatureScorerOverridesScorerMethods() {
final Class<?> ffsClass = Feature.FeatureWeight.FilterFeatureScorer.class;
for (final Method scorerClassMethod : Scorer.class.getDeclaredMethods()) {
final int modifiers = scorerClassMethod.getModifiers();
if (!Modifier.isAbstract(modifiers)) continue;
try {
final Method ossClassMethod = ossClass.getDeclaredMethod(
// the FilterFeatureScorer's implementation does not influence its parent Weight
if (scorerClassMethod.getName().equals("getWeight")) continue;
final Method ffsClassMethod = ffsClass.getDeclaredMethod(
scorerClassMethod.getName(),
scorerClassMethod.getParameterTypes());
assertEquals("getReturnType() difference",
scorerClassMethod.getReturnType(),
ossClassMethod.getReturnType());
ffsClassMethod.getReturnType());
} catch (NoSuchMethodException e) {
fail(ossClass + " needs to override '" + scorerClassMethod + "'");
fail(ffsClass + " needs to override '" + scorerClassMethod + "'");
}
}
}
}