Fix script score function that combines _score and weight (#22713)

The weight factor function does not check if the delegate score function needs to access the score of the query.
This results in a _score equals to 0 for all score function that set a weight.
This change modifies the WeightFactorFunction#needsScore to delegate the call to its underlying score function.

Fix #21483
This commit is contained in:
Jim Ferenczi 2017-01-20 19:50:57 +01:00 committed by GitHub
parent daf1f184d4
commit 4ec4bad908
2 changed files with 28 additions and 1 deletions

View File

@ -68,7 +68,7 @@ public class WeightFactorFunction extends ScoreFunction {
@Override
public boolean needsScores() {
return false;
return scoreFunction.needsScores();
}
public Explanation explainWeight() {

View File

@ -751,6 +751,33 @@ public class FunctionScoreTests extends ESTestCase {
assertThat(searchResult.scoreDocs[0].score, equalTo(explanation.getValue()));
}
public void testWeightFactorNeedsScore() {
for (boolean needsScore : new boolean[] {true, false}) {
WeightFactorFunction function = new WeightFactorFunction(10.0f, new ScoreFunction(CombineFunction.REPLACE) {
@Override
public LeafScoreFunction getLeafScoreFunction(LeafReaderContext ctx) throws IOException {
return null;
}
@Override
public boolean needsScores() {
return needsScore;
}
@Override
protected boolean doEquals(ScoreFunction other) {
return false;
}
@Override
protected int doHashCode() {
return 0;
}
});
assertEquals(needsScore, function.needsScores());
}
}
private static class DummyScoreFunction extends ScoreFunction {
protected DummyScoreFunction(CombineFunction scoreCombiner) {
super(scoreCombiner);