{"id":388,"date":"2011-12-19T21:56:29","date_gmt":"2011-12-19T20:56:29","guid":{"rendered":"http:\/\/sematext.solr.pl\/?p=388"},"modified":"2020-11-11T21:56:57","modified_gmt":"2020-11-11T20:56:57","slug":"do-i-have-to-look-for-maxbooleanclauses-when-using-filters","status":"publish","type":"post","link":"https:\/\/solr.pl\/en\/2011\/12\/19\/do-i-have-to-look-for-maxbooleanclauses-when-using-filters\/","title":{"rendered":"Do I have to look for maxBooleanClauses when using filters ?"},"content":{"rendered":"<p>One of the configuration variables we can find in the <em>solrconfig.xml<\/em> file is&nbsp;<em>maxBooleanClauses<\/em>, which specifies the maximum number of boolean clauses that can be combined in a single query. The question is, do I have to worry about it when using filters in Solr ? Let&#8217;s try to answer that question without getting into Lucene and Solr source code.<\/p>\n\n\n<!--more-->\n\n\n<h3>Query<\/h3>\n<p>Let&#8217;s assume that we have the following query we want to change:\n<\/p>\n<pre class=\"brush:xml\">q=category:1 AND category:2 AND category:3 ... AND category:2000<\/pre>\n<p>Sending such a query to the Solr instance with the default configuration would result in the following exception: &#8220;<em>too many boolean clauses<\/em>&#8220;. Of course we could modify the <em>maxBooleanClauses<\/em> variable in <em>solrconfig.xml<\/em> file and get rid of the exception, but let&#8217;s try do it the other way:<\/p>\n<h3>Let&#8217;s change the query to use filters<\/h3>\n<p>So, we change the above query to use filters &#8211; the <em>fq<\/em> parameter<em><\/em>:\n<\/p>\n<pre class=\"brush:xml\">q=*:*&amp;fq=category:(1 2 3 ... 2000)<\/pre>\n<p>We send the query to Solr and &#8230; and again the same situation happens &#8211; exception with the &#8220;<em>too many boolean clauses<\/em>&#8221; message. It happens because Solr has to &#8220;calculate&#8221; filter content and thus run the appropriate query. So, let&#8217;s modify the query once again:<\/p>\n<h3>Final query change<\/h3>\n<p>After the final modification our query should look like this:\n<\/p>\n<pre class=\"brush:xml\">q=*:*&amp;fq=category:1&amp;fq=category:2&amp;fq=category:3&amp;....&amp;fq=category:2000<\/pre>\n<p>After sending such a query we will get the search results (of course if there are documents matching the query in the index). This time Solr didn&#8217;t have to run a single, large boolean query and that&#8217;s why we didn&#8217;t exceed the<em> maxBooleanClauses<\/em> limit.<\/p>\n<h3>To sum up<\/h3>\n<p>As you can see the answer to the question asked in the begining of the post depend on the query we want to run. If our query is using <em>AND<\/em> boolean operator we can use <em>fq<\/em> parameter because multiple <em>fq<\/em> parameters are concatenated using <em>AND<\/em>. However if we have to use <em>OR<\/em> we would have to change the limit defined by <em>maxBooleanClauses<\/em> configuration variable. But please remember that changing this limit can have a negative effect on performance and memory usage.<\/p>","protected":false},"excerpt":{"rendered":"<p>One of the configuration variables we can find in the solrconfig.xml file is&nbsp;maxBooleanClauses, which specifies the maximum number of boolean clauses that can be combined in a single query. The question is, do I have to worry about it when<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[27],"tags":[433,435,434,181,162,436,165,164],"class_list":["post-388","post","type-post","status-publish","format-standard","hentry","category-solr-en","tag-bool","tag-boolean-2","tag-clause-2","tag-filter","tag-lucene-2","tag-maxbooleanclauses","tag-query-2","tag-solr-2"],"_links":{"self":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/comments?post=388"}],"version-history":[{"count":1,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/388\/revisions"}],"predecessor-version":[{"id":389,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/388\/revisions\/389"}],"wp:attachment":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/media?parent=388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/categories?post=388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/tags?post=388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}