Skip to content

Commit

Permalink
script_score query errors on negative scores (#53133)
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 and release notes.
  • Loading branch information
mayya-sharipova authored Mar 5, 2020
1 parent 8851fb2 commit 7e2a9f5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/reference/release-notes/7.7.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ Mapping::
* 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.
(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].
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 All @@ -153,4 +161,5 @@ public double execute(ExplanationHolder explanation) {
}
};
}

}

0 comments on commit 7e2a9f5

Please sign in to comment.