Skip to content

Apache Lucene open-source search software

LUCENE-5264: CommonTermsQuery ignores minMustMatch if only high freq terms are present

Simon Willnauerf283429ee105
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 6a72b63514c..a62c85f26a8 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -120,6 +120,10 @@ Bug Fixes
descriptor exhaustion, hit at unlucky times inside IndexWriter could
lead to silently losing deletions. (Shai Erera, Mike McCandless)
+* LUCENE-5264: CommonTermsQuery ignored minMustMatch if only high-frequent
+ terms were present in the query and the high-frequent operator was set
+ to SHOULD. (Simon Willnauer)
+
API Changes:
* LUCENE-5222: Add SortField.needsScores(). Previously it was not possible
diff --git a/lucene/queries/src/java/org/apache/lucene/queries/CommonTermsQuery.java b/lucene/queries/src/java/org/apache/lucene/queries/CommonTermsQuery.java
index c0389a79d99..925bb4e5843 100644
--- a/lucene/queries/src/java/org/apache/lucene/queries/CommonTermsQuery.java
+++ b/lucene/queries/src/java/org/apache/lucene/queries/CommonTermsQuery.java
@@ -214,18 +214,13 @@ public class CommonTermsQuery extends Query {
* if lowFreq is empty we rewrite the high freq terms in a conjunction to
* prevent slow queries.
*/
- if (highFreqOccur == Occur.MUST) {
- highFreq.setBoost(getBoost());
- return highFreq;
- } else {
- BooleanQuery highFreqConjunction = new BooleanQuery();
+ if (highFreq.getMinimumNumberShouldMatch() == 0 && highFreqOccur != Occur.MUST) {
for (BooleanClause booleanClause : highFreq) {
- highFreqConjunction.add(booleanClause.getQuery(), Occur.MUST);
+ booleanClause.setOccur(Occur.MUST);
}
- highFreqConjunction.setBoost(getBoost());
- return highFreqConjunction;
-
}
+ highFreq.setBoost(getBoost());
+ return highFreq;
} else if (highFreq.clauses().isEmpty()) {
// only do low freq terms - we don't have high freq terms
lowFreq.setBoost(getBoost());
diff --git a/lucene/queries/src/test/org/apache/lucene/queries/CommonTermsQueryTest.java b/lucene/queries/src/test/org/apache/lucene/queries/CommonTermsQueryTest.java
index 0dd11c82863..cc06eda31c9 100644
--- a/lucene/queries/src/test/org/apache/lucene/queries/CommonTermsQueryTest.java
+++ b/lucene/queries/src/test/org/apache/lucene/queries/CommonTermsQueryTest.java
@@ -283,6 +283,37 @@ public class CommonTermsQueryTest extends LuceneTestCase {
r.document(search.scoreDocs[1].doc).get("id"),
r.document(search.scoreDocs[2].doc).get("id"))));
}
+
+ {
+ // only high freq terms around - check that min should match is applied
+ CommonTermsQuery query = new CommonTermsQuery(Occur.SHOULD, Occur.SHOULD,
+ random().nextBoolean() ? 2.0f : 0.5f);
+ query.add(new Term("field", "is"));
+ query.add(new Term("field", "this"));
+ query.add(new Term("field", "the"));
+ query.setLowFreqMinimumNumberShouldMatch(1.0f);
+ query.setHighFreqMinimumNumberShouldMatch(2.0f);
+ TopDocs search = s.search(query, 10);
+ assertEquals(search.totalHits, 4);
+ }
+
+ {
+ // only high freq terms around - check that min should match is applied
+ CommonTermsQuery query = new CommonTermsQuery(Occur.MUST, Occur.SHOULD,
+ random().nextBoolean() ? 2.0f : 0.5f);
+ query.add(new Term("field", "is"));
+ query.add(new Term("field", "this"));
+ query.add(new Term("field", "the"));
+ query.setLowFreqMinimumNumberShouldMatch(1.0f);
+ query.setHighFreqMinimumNumberShouldMatch(2.0f);
+ TopDocs search = s.search(query, 10);
+ assertEquals(search.totalHits, 2);
+ assertEquals(
+ new HashSet<>(Arrays.asList("0", "2")),
+ new HashSet<>(Arrays.asList(
+ r.document(search.scoreDocs[0].doc).get("id"),
+ r.document(search.scoreDocs[1].doc).get("id"))));
+ }
r.close();
w.close();
dir.close();