Skip to content

Commit

Permalink
Improve error message for ln/log with negative results in function score
Browse files Browse the repository at this point in the history
This changes the error message for a negative result in a function score when 
using the ln modifier to suggest using ln1p or ln2p when a negative result 
occurs in a function score and for the log modifier to suggest using log1p or 
log2p.

This relates to #41509
  • Loading branch information
jdconrad authored May 2, 2019
1 parent e466fb2 commit bc9cdc2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,14 @@ public double score(int docId, float subQueryScore) throws IOException {
double val = value * boostFactor;
double result = modifier.apply(val);
if (result < 0f) {
throw new IllegalArgumentException("field value function must not produce negative scores, but got: [" +
result + "] for field value: [" + value + "]");
String message = "field value function must not produce negative scores, but got: " +
"[" + result + "] for field value: [" + value + "]";
if (modifier == Modifier.LN) {
message += "; consider using ln1p or ln2p instead of ln to avoid negative scores";
} else if (modifier == Modifier.LOG) {
message += "; consider using log1p or log2p instead of log to avoid negative scores";
}
throw new IllegalArgumentException(message);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNot.not;

public class FunctionScoreTests extends ESTestCase {

Expand Down Expand Up @@ -824,7 +825,6 @@ public void testWithInvalidScores() {
assertThat(exc.getMessage(), containsString("function score query returned an invalid score: " + Float.NEGATIVE_INFINITY));
}


public void testExceptionOnNegativeScores() {
IndexSearcher localSearcher = new IndexSearcher(reader);
TermQuery termQuery = new TermQuery(new Term(FIELD, "out"));
Expand All @@ -836,6 +836,34 @@ public void testExceptionOnNegativeScores() {
new FunctionScoreQuery(termQuery, fvfFunction, CombineFunction.REPLACE, null, Float.POSITIVE_INFINITY);
IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> localSearcher.search(fsQuery1, 1));
assertThat(exc.getMessage(), containsString("field value function must not produce negative scores"));
assertThat(exc.getMessage(), not(containsString("consider using ln1p or ln2p instead of ln to avoid negative scores")));
assertThat(exc.getMessage(), not(containsString("consider using log1p or log2p instead of log to avoid negative scores")));
}

public void testExceptionOnLnNegativeScores() {
IndexSearcher localSearcher = new IndexSearcher(reader);
TermQuery termQuery = new TermQuery(new Term(FIELD, "out"));

// test that field_value_factor function using modifier ln throws an exception on negative scores
FieldValueFactorFunction.Modifier modifier = FieldValueFactorFunction.Modifier.LN;
final ScoreFunction fvfFunction = new FieldValueFactorFunction(FIELD, 0.5f, modifier, 1.0, new IndexNumericFieldDataStub());
FunctionScoreQuery fsQuery1 =
new FunctionScoreQuery(termQuery, fvfFunction, CombineFunction.REPLACE, null, Float.POSITIVE_INFINITY);
IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> localSearcher.search(fsQuery1, 1));
assertThat(exc.getMessage(), containsString("consider using ln1p or ln2p instead of ln to avoid negative scores"));
}

public void testExceptionOnLogNegativeScores() {
IndexSearcher localSearcher = new IndexSearcher(reader);
TermQuery termQuery = new TermQuery(new Term(FIELD, "out"));

// test that field_value_factor function using modifier log throws an exception on negative scores
FieldValueFactorFunction.Modifier modifier = FieldValueFactorFunction.Modifier.LOG;
final ScoreFunction fvfFunction = new FieldValueFactorFunction(FIELD, 0.5f, modifier, 1.0, new IndexNumericFieldDataStub());
FunctionScoreQuery fsQuery1 =
new FunctionScoreQuery(termQuery, fvfFunction, CombineFunction.REPLACE, null, Float.POSITIVE_INFINITY);
IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> localSearcher.search(fsQuery1, 1));
assertThat(exc.getMessage(), containsString("consider using log1p or log2p instead of log to avoid negative scores"));
}

private static class DummyScoreFunction extends ScoreFunction {
Expand Down

0 comments on commit bc9cdc2

Please sign in to comment.