Skip to content

Apache Lucene open-source search software

LUCENE-8221: MoreLikeThis.setMaxDocFreqPct can easily int-overflow on larger indexes.

Dawid Weiss719fce802697
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 1b2ba844f0f..d1f5395ee5b 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -202,6 +202,9 @@ New Features
Bug Fixes
+* LUCENE-8221: MoreLikeThis.setMaxDocFreqPct can easily int-overflow on larger
+ indexes.
+
* LUCENE-8266: Detect bogus tiles when creating a standard polygon and
throw a TileException. (Ignacio Vera)
diff --git a/lucene/queries/src/java/org/apache/lucene/queries/mlt/MoreLikeThis.java b/lucene/queries/src/java/org/apache/lucene/queries/mlt/MoreLikeThis.java
index 9afa68708a6..e145f7405b7 100644
--- a/lucene/queries/src/java/org/apache/lucene/queries/mlt/MoreLikeThis.java
+++ b/lucene/queries/src/java/org/apache/lucene/queries/mlt/MoreLikeThis.java
@@ -418,14 +418,16 @@ public final class MoreLikeThis {
* Set the maximum percentage in which words may still appear. Words that appear
* in more than this many percent of all docs will be ignored.
*
+ * This method calls {@link #setMaxDocFreq(int)} internally (both conditions cannot
+ * be used at the same time).
+ *
* @param maxPercentage the maximum percentage of documents (0-100) that a term may appear
- * in to be still considered relevant
+ * in to be still considered relevant.
*/
public void setMaxDocFreqPct(int maxPercentage) {
- this.maxDocFreq = maxPercentage * ir.numDocs() / 100;
+ setMaxDocFreq(Math.toIntExact((long) maxPercentage * ir.numDocs() / 100));
}
-
/**
* Returns whether to boost terms in query based on "score" or not. The default is
* {@link #DEFAULT_BOOST}.