-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add minimal sanity checks to custom/scripted similarities. (#33564)
Add minimal sanity checks to custom/scripted similarities. Lucene 8 introduced more constraints on similarities, in particular: - scores must not be negative, - scores must not decrease when term freq increases, - scores must not increase when norm (interpreted as an unsigned long) increases. We can't check every single case, but could at least run some sanity checks. Relates #33309
- Loading branch information
Showing
6 changed files
with
339 additions
and
10 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
server/src/main/java/org/elasticsearch/index/similarity/NonNegativeScoresSimilarity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.index.similarity; | ||
|
||
import org.apache.lucene.index.FieldInvertState; | ||
import org.apache.lucene.search.CollectionStatistics; | ||
import org.apache.lucene.search.Explanation; | ||
import org.apache.lucene.search.TermStatistics; | ||
import org.apache.lucene.search.similarities.Similarity; | ||
|
||
/** | ||
* A {@link Similarity} that rejects negative scores. This class exists so that users get | ||
* an error instead of silently corrupt top hits. It should be applied to any custom or | ||
* scripted similarity. | ||
*/ | ||
// public for testing | ||
public final class NonNegativeScoresSimilarity extends Similarity { | ||
|
||
// Escape hatch | ||
private static final String ES_ENFORCE_POSITIVE_SCORES = "es.enforce.positive.scores"; | ||
private static final boolean ENFORCE_POSITIVE_SCORES; | ||
static { | ||
String enforcePositiveScores = System.getProperty(ES_ENFORCE_POSITIVE_SCORES); | ||
if (enforcePositiveScores == null) { | ||
ENFORCE_POSITIVE_SCORES = true; | ||
} else if ("false".equals(enforcePositiveScores)) { | ||
ENFORCE_POSITIVE_SCORES = false; | ||
} else { | ||
throw new IllegalArgumentException(ES_ENFORCE_POSITIVE_SCORES + " may only be unset or set to [false], but got [" + | ||
enforcePositiveScores + "]"); | ||
} | ||
} | ||
|
||
private final Similarity in; | ||
|
||
public NonNegativeScoresSimilarity(Similarity in) { | ||
this.in = in; | ||
} | ||
|
||
public Similarity getDelegate() { | ||
return in; | ||
} | ||
|
||
@Override | ||
public long computeNorm(FieldInvertState state) { | ||
return in.computeNorm(state); | ||
} | ||
|
||
@Override | ||
public SimScorer scorer(float boost, CollectionStatistics collectionStats, TermStatistics... termStats) { | ||
final SimScorer inScorer = in.scorer(boost, collectionStats, termStats); | ||
return new SimScorer() { | ||
|
||
@Override | ||
public float score(float freq, long norm) { | ||
float score = inScorer.score(freq, norm); | ||
if (score < 0f) { | ||
if (ENFORCE_POSITIVE_SCORES) { | ||
throw new IllegalArgumentException("Similarities must not produce negative scores, but got:\n" + | ||
inScorer.explain(Explanation.match(freq, "term frequency"), norm)); | ||
} else { | ||
return 0f; | ||
} | ||
} | ||
return score; | ||
} | ||
|
||
@Override | ||
public Explanation explain(Explanation freq, long norm) { | ||
Explanation expl = inScorer.explain(freq, norm); | ||
if (expl.isMatch() && expl.getValue().floatValue() < 0) { | ||
expl = Explanation.match(0f, "max of:", | ||
expl, Explanation.match(0f, "Minimum allowed score")); | ||
} | ||
return expl; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...er/src/test/java/org/elasticsearch/index/similarity/NonNegativeScoresSimilarityTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.index.similarity; | ||
|
||
import org.apache.lucene.index.FieldInvertState; | ||
import org.apache.lucene.search.CollectionStatistics; | ||
import org.apache.lucene.search.TermStatistics; | ||
import org.apache.lucene.search.similarities.Similarity; | ||
import org.apache.lucene.search.similarities.Similarity.SimScorer; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.hamcrest.Matchers; | ||
|
||
public class NonNegativeScoresSimilarityTests extends ESTestCase { | ||
|
||
public void testBasics() { | ||
Similarity negativeScoresSim = new Similarity() { | ||
|
||
@Override | ||
public long computeNorm(FieldInvertState state) { | ||
return state.getLength(); | ||
} | ||
|
||
@Override | ||
public SimScorer scorer(float boost, CollectionStatistics collectionStats, TermStatistics... termStats) { | ||
return new SimScorer() { | ||
@Override | ||
public float score(float freq, long norm) { | ||
return freq - 5; | ||
} | ||
}; | ||
} | ||
}; | ||
Similarity assertingSimilarity = new NonNegativeScoresSimilarity(negativeScoresSim); | ||
SimScorer scorer = assertingSimilarity.scorer(1f, null); | ||
assertEquals(2f, scorer.score(7f, 1L), 0f); | ||
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> scorer.score(2f, 1L)); | ||
assertThat(e.getMessage(), Matchers.containsString("Similarities must not produce negative scores")); | ||
} | ||
|
||
} |
Oops, something went wrong.