figuring out reason for maxClauseCount is set to 1024 error

蓝咒 提交于 2019-12-05 00:30:51

The range query essentially gets transformed to a boolean query with one clause for every possible value, ORed together.

For example, the query +price:[10 to 13] is tranformed to a boolean query

+(price:10 price:11 price:12 price:13)

assuming all the values 10-13 exist in the index.

I suppose, all of your 1300 values fall in the range you have given. So, boolean query has 1300 clauses, which is higher than the default value of 1024. In the test index, the limit of 1024 is not reached as there are only 950 values.

I had the same problem. My solution was to catch BooleanQuery.TooManyClauses and dynamically increase maxClauseCount.

Here is some code that is similar to what I have in production.

Good Luck, Randy


    private static Hits searchIndex(Searcher searcher, Query query)
        throws IOException
    {
        boolean retry = true;
        while (retry)
        {
            try
            {
                retry = false;
                Hits myHits = searcher.search(query);
                return myHits;
            }
            catch (BooleanQuery.TooManyClauses e)
            {
                // Double the number of boolean queries allowed.
                // The default is in org.apache.lucene.search.BooleanQuery and is 1024.
                String defaultQueries = Integer.toString(BooleanQuery.getMaxClauseCount());
                int oldQueries = Integer.parseInt(System.getProperty("org.apache.lucene.maxClauseCount", defaultQueries));
                int newQueries = oldQueries * 2;
                log.error("Too many hits for query: " + oldQueries + ".  Increasing to " + newQueries, e);
                System.setProperty("org.apache.lucene.maxClauseCount", Integer.toString(newQueries));
                BooleanQuery.setMaxClauseCount(newQueries);
                retry = true;
            }
        }
    }

I had this same issue in C# code running with the Sitecore web content management system. I used Randy's answer above, but was not able to use the System get and set property functionality. Instead I retrieved the current count, incremented it, and set it back. Worked great!

catch (BooleanQuery.TooManyClauses e)
{
    // Increment the number of boolean queries allowed.
    // The default is 1024.
    var currMaxClause = BooleanQuery.GetMaxClauseCount();
    var newMaxClause = currMaxClause + 1024;
    BooleanQuery.SetMaxClauseCount(newMaxClause);
    retry = true;
}

Add This Code

@using Lucene.Net.Search;
@BooleanQuery.SetMaxClauseCount(2048);

Just put, BooleanQuery.setMaxClauseCount( Integer.MAX_VALUE );and that's it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!