Skip to content

Commit

Permalink
Add test script_score error on negative score
Browse files Browse the repository at this point in the history
7.5 and 7.6 had a regression that allowed for
script_score queries to have negative scores.
We have corrected this regression in #52478.
This is an addition to #52478 that adds a test for this.

Related to #53133
  • Loading branch information
mayya-sharipova committed Mar 5, 2020
1 parent 9e87913 commit 6f9050e
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
import static org.mockito.Mockito.when;

public class ScriptScoreQueryTests extends ESTestCase {

private Directory dir;
private IndexWriter w;
private DirectoryReader reader;
Expand Down Expand Up @@ -131,6 +131,14 @@ public void testExplainDefaultNoScore() throws IOException {
assertThat(explanation.getValue(), equalTo(2.0f));
}

public void testScriptScoreErrorOnNegativeScore() {
Script script = new Script("script that returns a negative score");
ScoreScript.LeafFactory factory = newFactory(script, false, explanation -> -1000.0);
ScriptScoreQuery query = new ScriptScoreQuery(Queries.newMatchAllQuery(), script, factory, null, "index", 0, Version.CURRENT);
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> searcher.search(query, 1));
assertTrue(e.getMessage().contains("Must be a non-negative score!"));
}

private ScoreScript.LeafFactory newFactory(Script script, boolean needsScore,
Function<ScoreScript.ExplanationHolder, Double> function) {
SearchLookup lookup = mock(SearchLookup.class);
Expand Down

0 comments on commit 6f9050e

Please sign in to comment.