Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me this set of unit tests is quite straightforward + easy to understand. I think yml tests are slower to debug and are most helpful for testing the end-to-end integration. We recently added a set of testing guidelines that I think are really helpful: https://github.com/elastic/elasticsearch/blob/master/TESTING.asciidoc#what-kind-of-tests-should-i-write

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jtibshirani Thanks a lot, I missed these guidelines, will study them

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) {
}
};
}

}