Skip to content

Commit 6f9050e

Browse files
Add test script_score error on negative score
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
1 parent 9e87913 commit 6f9050e

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
import static org.mockito.Mockito.when;
5454

5555
public class ScriptScoreQueryTests extends ESTestCase {
56-
56+
5757
private Directory dir;
5858
private IndexWriter w;
5959
private DirectoryReader reader;
@@ -131,6 +131,14 @@ public void testExplainDefaultNoScore() throws IOException {
131131
assertThat(explanation.getValue(), equalTo(2.0f));
132132
}
133133

134+
public void testScriptScoreErrorOnNegativeScore() {
135+
Script script = new Script("script that returns a negative score");
136+
ScoreScript.LeafFactory factory = newFactory(script, false, explanation -> -1000.0);
137+
ScriptScoreQuery query = new ScriptScoreQuery(Queries.newMatchAllQuery(), script, factory, null, "index", 0, Version.CURRENT);
138+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> searcher.search(query, 1));
139+
assertTrue(e.getMessage().contains("Must be a non-negative score!"));
140+
}
141+
134142
private ScoreScript.LeafFactory newFactory(Script script, boolean needsScore,
135143
Function<ScoreScript.ExplanationHolder, Double> function) {
136144
SearchLookup lookup = mock(SearchLookup.class);

0 commit comments

Comments
 (0)