Skip to content

Commit 7e2a9f5

Browse files
script_score query errors on negative scores (#53133)
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 and release notes.
1 parent 8851fb2 commit 7e2a9f5

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

docs/reference/release-notes/7.7.asciidoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ Mapping::
1111
* Dynamic mappings in indices created on 8.0 and later have stricter validation at mapping update time and
1212
results in a deprecation warning for indices created in Elasticsearch 7.7.0 and later.
1313
(e.g. incorrect analyzer settings or unknown field types). {pull}51233[#51233]
14+
15+
Search::
16+
* A regression that allowed negative scores in a script_score query was corrected.
17+
A behaviour is restored that throws an error if script_score query produces
18+
a negative score {pull}52478[#52478].

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

Lines changed: 10 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);
@@ -153,4 +161,5 @@ public double execute(ExplanationHolder explanation) {
153161
}
154162
};
155163
}
164+
156165
}

0 commit comments

Comments
 (0)