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.
This commit is contained in:
Mayya Sharipova 2020-03-05 14:23:39 -05:00 committed by GitHub
parent 8851fb2a08
commit 7e2a9f58ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -11,3 +11,8 @@ Mapping::
* Dynamic mappings in indices created on 8.0 and later have stricter validation at mapping update time and * Dynamic mappings in indices created on 8.0 and later have stricter validation at mapping update time and
results in a deprecation warning for indices created in Elasticsearch 7.7.0 and later. results in a deprecation warning for indices created in Elasticsearch 7.7.0 and later.
(e.g. incorrect analyzer settings or unknown field types). {pull}51233[#51233] (e.g. incorrect analyzer settings or unknown field types). {pull}51233[#51233]
Search::
* A regression that allowed negative scores in a script_score query was corrected.
A behaviour is restored that throws an error if script_score query produces
a negative score {pull}52478[#52478].

View File

@ -53,7 +53,7 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
public class ScriptScoreQueryTests extends ESTestCase { public class ScriptScoreQueryTests extends ESTestCase {
private Directory dir; private Directory dir;
private IndexWriter w; private IndexWriter w;
private DirectoryReader reader; private DirectoryReader reader;
@ -131,6 +131,14 @@ public class ScriptScoreQueryTests extends ESTestCase {
assertThat(explanation.getValue(), equalTo(2.0f)); 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, private ScoreScript.LeafFactory newFactory(Script script, boolean needsScore,
Function<ScoreScript.ExplanationHolder, Double> function) { Function<ScoreScript.ExplanationHolder, Double> function) {
SearchLookup lookup = mock(SearchLookup.class); SearchLookup lookup = mock(SearchLookup.class);
@ -153,4 +161,5 @@ public class ScriptScoreQueryTests extends ESTestCase {
} }
}; };
} }
} }