Apache Lucene open-source search software
LUCENE-6982: throw IAE on illegal bm25 parameter values
Robert Muirfb0aa2dc49cb
diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/BM25Similarity.java b/lucene/core/src/java/org/apache/lucene/search/similarities/BM25Similarity.javaindex da00fa3bb6a..5891a611b80 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/BM25Similarity.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/BM25Similarity.java@@ -44,8 +44,16 @@ public class BM25Similarity extends Similarity {* BM25 with the supplied parameter values.* @param k1 Controls non-linear term frequency normalization (saturation).* @param b Controls to what degree document length normalizes tf values.+ * @throws IllegalArgumentException if {@code k1} is infinite or negative, or if {@code b} is+ * not within the range {@code [0..1]}*/public BM25Similarity(float k1, float b) {+ if (Float.isFinite(k1) == false || k1 < 0) {+ throw new IllegalArgumentException("illegal k1 value: " + k1 + ", must be a non-negative finite value");+ }+ if (Float.isNaN(b) || b < 0 || b > 1) {+ throw new IllegalArgumentException("illegal b value: " + b + ", must be between 0 and 1");+ }this.k1 = k1;this.b = b;}@@ -57,8 +65,7 @@ public class BM25Similarity extends Similarity {* </ul>*/public BM25Similarity() {- this.k1 = 1.2f;- this.b = 0.75f;+ this(1.2f, 0.75f);}/** Implemented as <code>log(1 + (docCount - docFreq + 0.5)/(docFreq + 0.5))</code>. */diff --git a/lucene/core/src/test/org/apache/lucene/search/similarities/TestBM25Similarity.java b/lucene/core/src/test/org/apache/lucene/search/similarities/TestBM25Similarity.javaindex d6b6ff7ac33..68be891931c 100644--- a/lucene/core/src/test/org/apache/lucene/search/similarities/TestBM25Similarity.java+++ b/lucene/core/src/test/org/apache/lucene/search/similarities/TestBM25Similarity.java@@ -33,4 +33,50 @@ public class TestBM25Similarity extends LuceneTestCase {}}}++ public void testIllegalK1() {+ try {+ new BM25Similarity(Float.POSITIVE_INFINITY, 0.75f);+ } catch (IllegalArgumentException expected) {+ assertTrue(expected.getMessage().contains("illegal k1 value"));+ }++ try {+ new BM25Similarity(-1, 0.75f);+ } catch (IllegalArgumentException expected) {+ assertTrue(expected.getMessage().contains("illegal k1 value"));+ }++ try {+ new BM25Similarity(Float.NaN, 0.75f);+ } catch (IllegalArgumentException expected) {+ assertTrue(expected.getMessage().contains("illegal k1 value"));+ }+ }++ public void testIllegalB() {+ try {+ new BM25Similarity(1.2f, 2f);+ } catch (IllegalArgumentException expected) {+ assertTrue(expected.getMessage().contains("illegal b value"));+ }++ try {+ new BM25Similarity(1.2f, -1f);+ } catch (IllegalArgumentException expected) {+ assertTrue(expected.getMessage().contains("illegal b value"));+ }++ try {+ new BM25Similarity(1.2f, Float.POSITIVE_INFINITY);+ } catch (IllegalArgumentException expected) {+ assertTrue(expected.getMessage().contains("illegal b value"));+ }++ try {+ new BM25Similarity(1.2f, Float.NaN);+ } catch (IllegalArgumentException expected) {+ assertTrue(expected.getMessage().contains("illegal b value"));+ }+ }}