<p>I've had an app doing prefix searches for a while. Recently the index size was increased and it turned out that some prefixes were too darned numerous for lucene to handle.  It kept throwing me a <a href="http://web.archive.org/web/20080901205009/http://lucene.apache.org:80/java/2_3_2/api/core/org/apache/lucene/search/BooleanQuery.TooManyClauses.html" rel="nofollow noreferrer">Too Many Clauses</a> error, which was very frustrating as I kept looking at my JARs and confirming that none of the included code actually used a boolean query.</p>
<p>Why doesn't it throw something like a Too Many Hits exception? And why does increasing the boolean query's static max clauses integer actually make this error go away, when I'm definitely only using a prefix query? Is there something fundamental to how queries are run that I'm not understanding; is it that they secretly become Boolean queries?</p>

[{'answer_id': 9085, 'author': 'Ryan Ahearn', 'author_id': 75, 'author_profile': 'https://Stackoverflow.com/users/75', 'pm_score': 4, 'selected': True, 'text': '<p>I\'ve hit this before.  It has to do with the fact that lucene, under the covers, turns many (all?) things into boolean queries when you call Query.rewrite()</p>\n<p>From: <a href="http://web.archive.org/web/20110915061619/http://lucene.apache.org:80/java/2_2_0/api/org/apache/lucene/search/Query.html" rel="nofollow noreferrer">http://web.archive.org/web/20110915061619/http://lucene.apache.org:80/java/2_2_0/api/org/apache/lucene/search/Query.html</a></p>\n<pre><code>public Query rewrite(IndexReader reader)\n              throws IOException\n\n    Expert: called to re-write queries into primitive queries.\n            For example, a PrefixQuery will be rewritten into a\n            BooleanQuery that consists of TermQuerys.\n\n    Throws:\n        IOException\n</code></pre>\n'}, {'answer_id': 66419, 'author': 'Stefan Schultze', 'author_id': 6358, 'author_profile': 'https://Stackoverflow.com/users/6358', 'pm_score': 0, 'selected': False, 'text': '<p>When running a prefix query, Lucene searches for all terms in its "dictionary" that match the query. If more than 1024 (by default) match, the TooManyClauses-Exception is thrown.</p>\n\n<p>You can call BooleanQuery.setMaxClauseCount to increase the maximum number of clauses permitted per BooleanQuery.</p>\n'}, {'answer_id': 150553, 'author': 'Kai Chan', 'author_id': 23589, 'author_profile': 'https://Stackoverflow.com/users/23589', 'pm_score': 2, 'selected': False, 'text': '<p>The API reference page of TooManyClauses shows that PrefixQuery, FuzzyQuery, WildcardQuery, and RangeQuery are expanded this way (into BooleanQuery). Since it is in the API reference, it should be a behavior that users can rely on. Lucene does not place arbitrary limits on the number of hits (other than a document ID being an int) so a "too many hits" exception might not make sense. Perhaps PrefixQuery.rewrite(IndexReader) should catch the TooManyClauses and throw a "too many prefixes" exception, but right now it does not behave that way.</p>\n\n<p>By the way, another way to search by prefix is to use PrefixFilter. Either filter your query with it or wrap the filter with a ConstantScoreQuery.</p>\n'}]
['https://Stackoverflow.com/questions/8532', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/459/']