Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-8479: QueryBuilder#analyzeGraphPhrase now throws TooManyClause exception if the number of expanded path reaches the BooleanQuery#maxClause limit.

Jim Ferenczi0f100004bcd8
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 3f23c1ecdca..ad83ff9799e 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -181,12 +181,17 @@ Optimizations
======================= Lucene 7.6.0 =======================
-Build
+Build:
* LUCENE-8504: Upgrade forbiddenapis to version 2.6. (Uwe Schindler)
* LUCENE-8493: Stop publishing insecure .sha1 files with releases (janhoy)
+Bug fixes:
+
+* LUCENE-8479: QueryBuilder#analyzeGraphPhrase now throws TooManyClause exception
+ if the number of expanded path reaches the BooleanQuery#maxClause limit. (Jim Ferenczi)
+
======================= Lucene 7.5.1 =======================
Bug Fixes:
diff --git a/lucene/core/src/java/org/apache/lucene/util/QueryBuilder.java b/lucene/core/src/java/org/apache/lucene/util/QueryBuilder.java
index 697e3bb6657..37b7e3ed922 100644
--- a/lucene/core/src/java/org/apache/lucene/util/QueryBuilder.java
+++ b/lucene/core/src/java/org/apache/lucene/util/QueryBuilder.java
@@ -552,6 +552,7 @@ public class QueryBuilder {
List<SpanQuery> clauses = new ArrayList<>();
int[] articulationPoints = graph.articulationPoints();
int lastState = 0;
+ int maxClauseCount = BooleanQuery.getMaxClauseCount();
for (int i = 0; i <= articulationPoints.length; i++) {
int start = lastState;
int end = -1;
@@ -567,6 +568,9 @@ public class QueryBuilder {
TokenStream ts = it.next();
SpanQuery q = createSpanQuery(ts, field);
if (q != null) {
+ if (queries.size() >= maxClauseCount) {
+ throw new BooleanQuery.TooManyClauses();
+ }
queries.add(q);
}
}
@@ -581,6 +585,9 @@ public class QueryBuilder {
if (terms.length == 1) {
queryPos = new SpanTermQuery(terms[0]);
} else {
+ if (terms.length >= maxClauseCount) {
+ throw new BooleanQuery.TooManyClauses();
+ }
SpanTermQuery[] orClauses = new SpanTermQuery[terms.length];
for (int idx = 0; idx < terms.length; idx++) {
orClauses[idx] = new SpanTermQuery(terms[idx]);
@@ -591,6 +598,9 @@ public class QueryBuilder {
}
if (queryPos != null) {
+ if (clauses.size() >= maxClauseCount) {
+ throw new BooleanQuery.TooManyClauses();
+ }
clauses.add(queryPos);
}
}
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestQueryBuilder.java b/lucene/core/src/test/org/apache/lucene/util/TestQueryBuilder.java
index fece16697cf..4af077bde46 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestQueryBuilder.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestQueryBuilder.java
@@ -18,8 +18,10 @@ package org.apache.lucene.util;
import java.io.IOException;
+import java.io.Reader;
import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.CannedBinaryTokenStream;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.analysis.MockSynonymFilter;
import org.apache.lucene.analysis.MockTokenizer;
@@ -443,4 +445,31 @@ public class TestQueryBuilder extends LuceneTestCase {
QueryBuilder builder = new QueryBuilder(analyzer);
assertNull(builder.createBooleanQuery("field", "whatever"));
}
+
+ public void testMaxBooleanClause() throws Exception {
+ int size = 34;
+ CannedBinaryTokenStream.BinaryToken[] tokens = new CannedBinaryTokenStream.BinaryToken[size];
+ BytesRef term1 = new BytesRef("ff");
+ BytesRef term2 = new BytesRef("f");
+ for (int i = 0; i < size;) {
+ if (i % 2 == 0) {
+ tokens[i] = new CannedBinaryTokenStream.BinaryToken(term2, 1, 1);
+ tokens[i + 1] = new CannedBinaryTokenStream.BinaryToken(term1, 0, 2);
+ i += 2;
+ } else {
+ tokens[i] = new CannedBinaryTokenStream.BinaryToken(term2, 1, 1);
+ i ++;
+ }
+ }
+ QueryBuilder qb = new QueryBuilder(null);
+ try (TokenStream ts = new CannedBinaryTokenStream(tokens)) {
+ expectThrows(BooleanQuery.TooManyClauses.class, () -> qb.analyzeGraphBoolean("", ts, BooleanClause.Occur.MUST));
+ }
+ try (TokenStream ts = new CannedBinaryTokenStream(tokens)) {
+ expectThrows(BooleanQuery.TooManyClauses.class, () -> qb.analyzeGraphBoolean("", ts, BooleanClause.Occur.SHOULD));
+ }
+ try (TokenStream ts = new CannedBinaryTokenStream(tokens)) {
+ expectThrows(BooleanQuery.TooManyClauses.class, () -> qb.analyzeGraphPhrase(ts, "", 0));
+ }
+ }
}