Skip to content

Commit

Permalink
Make "too many clauses" throw IllegalArgumentException to avoid 500s
Browse files Browse the repository at this point in the history
  • Loading branch information
smalyshev committed Sep 16, 2024
1 parent a016f78 commit 48b04ae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ public Query rewrite(Query original) throws IOException {
} catch (TimeExceededException e) {
timeExceeded = true;
return new MatchNoDocsQuery("rewrite timed out");
} catch (TooManyClauses e) {
throw new IllegalArgumentException("Query rewrite failed: too many clauses", e);
} finally {
if (profiler != null) {
profiler.stopAndAddRewriteTime(rewriteTimer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.lucene.index.Term;
import org.apache.lucene.queries.spans.SpanNearQuery;
import org.apache.lucene.queries.spans.SpanTermQuery;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Collector;
Expand Down Expand Up @@ -1105,6 +1106,22 @@ private static ContextIndexSearcher newContextSearcher(IndexReader reader) throw
);
}

public void testTooManyClauses() throws Exception {
indexDocs();
var oldCount = IndexSearcher.getMaxClauseCount();
try {
var query = new BooleanQuery.Builder().add(new BooleanClause(new MatchAllDocsQuery(), Occur.SHOULD))
.add(new MatchAllDocsQuery(), Occur.SHOULD)
.build();
try (TestSearchContext context = createContext(newContextSearcher(reader), query)) {
IndexSearcher.setMaxClauseCount(1);
expectThrows(IllegalArgumentException.class, context::rewrittenQuery);
}
} finally {
IndexSearcher.setMaxClauseCount(oldCount);
}
}

private static ContextIndexSearcher noCollectionContextSearcher(IndexReader reader) throws IOException {
return earlyTerminationContextSearcher(reader, 0);
}
Expand Down

0 comments on commit 48b04ae

Please sign in to comment.