Skip to content

fix(search): long queries no longer throw TooManyNestedClauses - #135

Merged
kdroidFilter merged 1 commit into
masterfrom
fix/long-query-clause-limit
Oct 21, 2026
Merged

kdroidFilter merged 1 commit into
masterfrom
fix/long-query-clause-limit

Conversation

@kdroidFilter

Copy link
Copy Markdown
Owner

Problem

A lexical query of many words — a whole sentence, such as a question typed in free language — can throw IndexSearcher.TooManyNestedClauses (maxClauseCount is set to 1024):

  • each token brings up to MAX_SYNONYM_TERMS_PER_TOKEN (32) dictionary forms, listed in the presence filter and in each of the four phrase queries (synonym phrase + the three slop phrases);
  • the magic/synonym boosts add up to MAX_SYNONYM_BOOST_TERMS (256) forms of each kind.

With ten or so words the query passes Lucene's default 1024 clauses and the search fails instead of returning results.

Fix

  • The forms per token now share a budget across the query's tokens (SYNONYM_TERMS_BUDGET = 160): unchanged up to five tokens (32 each), fewer beyond, at least four. Short queries behave exactly as before.
  • LuceneSearchEngine raises Lucene's clause limit to 4096 (only if lower), which leaves room for the boosts.

Verification

555 question-length Hebrew queries (6 to 20 words) through openSession(...).nextPage(30): none fails, 230 ms on average. Before the fix, such queries overflowed the limit.

A query of many words (a whole sentence, e.g. a question) can fail with
IndexSearcher.TooManyNestedClauses: each token brings up to 32 dictionary
forms, listed in the presence filter and in each of the four phrase
queries, and the synonym boosts add up to 256 forms of each kind, past
Lucene's default limit of 1024 clauses.

- The forms per token now share a budget across the query's tokens
  (unchanged up to five tokens, fewer beyond, at least four).
- The engine raises Lucene's clause limit to 4096 for the boosts.

Checked on 555 question-length queries (Hebrew questions of 6 to 20
words): none fails now, 230 ms on average; before, they overflowed.

Signed-off-by: Elie Gambache <elyahou.hadass@gmail.com>
@kdroidFilter
kdroidFilter merged commit 0749fbd into master Oct 21, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant