mirror of https://github.com/apache/lucene.git
LUCENE-8810: Honor MaxClausesCount in BooleanQuery (#787)
During Flattening, BooleanQuery will always try to flatten nested clauses during rewrite. However, this can cause the maximum number of clauses to be violated by the new query. This commit disables flattening in the specific case.
This commit is contained in:
parent
2885a11578
commit
a94093102e
|
@ -481,6 +481,7 @@ public class BooleanQuery extends Query implements Iterable<BooleanClause> {
|
|||
BooleanQuery.Builder builder = new BooleanQuery.Builder();
|
||||
builder.setMinimumNumberShouldMatch(minimumNumberShouldMatch);
|
||||
boolean actuallyRewritten = false;
|
||||
try {
|
||||
for (BooleanClause clause : clauses) {
|
||||
if (clause.getOccur() == Occur.SHOULD && clause.getQuery() instanceof BooleanQuery) {
|
||||
BooleanQuery innerQuery = (BooleanQuery) clause.getQuery();
|
||||
|
@ -499,6 +500,9 @@ public class BooleanQuery extends Query implements Iterable<BooleanClause> {
|
|||
if (actuallyRewritten) {
|
||||
return builder.build();
|
||||
}
|
||||
} catch (TooManyClauses exception) {
|
||||
// No-op : Do not flatten when the new query will violate max clause count
|
||||
}
|
||||
}
|
||||
|
||||
return super.rewrite(reader);
|
||||
|
|
|
@ -598,4 +598,19 @@ public class TestBooleanRewrites extends LuceneTestCase {
|
|||
w.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
public void testFlattenInnerDisjunctionsWithMoreThan1024Terms() throws IOException {
|
||||
IndexSearcher searcher = newSearcher(new MultiReader());
|
||||
|
||||
BooleanQuery.Builder builder1024 = new BooleanQuery.Builder();
|
||||
for(int i = 0; i < 1024; i++) {
|
||||
builder1024.add(new TermQuery(new Term("foo", "bar-" + i)), Occur.SHOULD);
|
||||
}
|
||||
Query inner = builder1024.build();
|
||||
Query query = new BooleanQuery.Builder()
|
||||
.add(inner, Occur.SHOULD)
|
||||
.add(new TermQuery(new Term("foo", "baz")), Occur.SHOULD)
|
||||
.build();
|
||||
assertSame(query, searcher.rewrite(query));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue