From ebad5443b27dee81ddb3e349ec12cac7e8b67280 Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Mon, 15 Nov 2010 10:15:39 +0000 Subject: [PATCH] LUCENE-2757, LUCENE-2690: Add test for BQ.maxClauseCount checking. Also the original test was missing duplicate terms in the MultiReader case, added this. This also fixes a "bug" in the TooManyClauses exception which can be seen in failing tests: The message may not contain the value of maxClauses at the time the exception was thrown. Tests have exactly this problem, because they print the message when the default is already restored. git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1035205 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/lucene/search/BooleanQuery.java | 6 +- .../search/TestMultiTermQueryRewrites.java | 62 +++++++++++++++++-- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/lucene/src/java/org/apache/lucene/search/BooleanQuery.java b/lucene/src/java/org/apache/lucene/search/BooleanQuery.java index d1f3ebcaeb4..8dd7cd09042 100644 --- a/lucene/src/java/org/apache/lucene/search/BooleanQuery.java +++ b/lucene/src/java/org/apache/lucene/search/BooleanQuery.java @@ -39,10 +39,8 @@ public class BooleanQuery extends Query implements Iterable { * is expanded to many terms during search. */ public static class TooManyClauses extends RuntimeException { - public TooManyClauses() {} - @Override - public String getMessage() { - return "maxClauseCount is set to " + maxClauseCount; + public TooManyClauses() { + super("maxClauseCount is set to " + maxClauseCount); } } diff --git a/lucene/src/test/org/apache/lucene/search/TestMultiTermQueryRewrites.java b/lucene/src/test/org/apache/lucene/search/TestMultiTermQueryRewrites.java index 13f1f640195..e37c8cbab5b 100644 --- a/lucene/src/test/org/apache/lucene/search/TestMultiTermQueryRewrites.java +++ b/lucene/src/test/org/apache/lucene/search/TestMultiTermQueryRewrites.java @@ -38,8 +38,8 @@ import java.io.IOException; public class TestMultiTermQueryRewrites extends LuceneTestCase { static Directory dir, sdir1, sdir2; - static IndexReader reader, multiReader; - static IndexSearcher searcher, multiSearcher; + static IndexReader reader, multiReader, multiReaderDupls; + static IndexSearcher searcher, multiSearcher, multiSearcherDupls; @BeforeClass public static void beforeClass() throws Exception { @@ -61,19 +61,26 @@ public class TestMultiTermQueryRewrites extends LuceneTestCase { reader = IndexReader.open(dir, true); searcher = new IndexSearcher(reader); + multiReader = new MultiReader(new IndexReader[] { IndexReader.open(sdir1, true), IndexReader.open(sdir2, true) }, true); multiSearcher = new IndexSearcher(multiReader); + + multiReaderDupls = new MultiReader(new IndexReader[] { + IndexReader.open(sdir1, true), IndexReader.open(dir, true) + }, true); + multiSearcherDupls = new IndexSearcher(multiReaderDupls); } @AfterClass public static void afterClass() throws Exception { reader.close(); multiReader.close(); + multiReaderDupls.close(); dir.close(); sdir1.close(); sdir2.close(); - reader = multiReader = null; - searcher = multiSearcher = null; + reader = multiReader = multiReaderDupls = null; + searcher = multiSearcher = multiSearcherDupls = null; dir = sdir1 = sdir2 = null; } @@ -108,14 +115,18 @@ public class TestMultiTermQueryRewrites extends LuceneTestCase { mtq.setRewriteMethod(method); final Query q1 = searcher.rewrite(mtq); final Query q2 = multiSearcher.rewrite(mtq); + final Query q3 = multiSearcherDupls.rewrite(mtq); if (VERBOSE) { System.out.println(); System.out.println("single segment: " + q1); - System.out.println(" multi segment: " + q2); + System.out.println("multi segment: " + q2); + System.out.println("multi segment with duplicates: " + q3); } assertEquals("The multi-segment case must produce same rewritten query", q1, q2); + assertEquals("The multi-segment case with duplicates must produce same rewritten query", q1, q3); checkBooleanQueryOrder(q1); checkBooleanQueryOrder(q2); + checkBooleanQueryOrder(q3); } public void testRewritesWithDuplicateTerms() throws Exception { @@ -166,14 +177,18 @@ public class TestMultiTermQueryRewrites extends LuceneTestCase { mtq.setRewriteMethod(method); final Query q1 = searcher.rewrite(mtq); final Query q2 = multiSearcher.rewrite(mtq); + final Query q3 = multiSearcherDupls.rewrite(mtq); if (VERBOSE) { System.out.println(); System.out.println("single segment: " + q1); - System.out.println(" multi segment: " + q2); + System.out.println("multi segment: " + q2); + System.out.println("multi segment with duplicates: " + q3); } assertEquals("The multi-segment case must produce same rewritten query", q1, q2); + assertEquals("The multi-segment case with duplicates must produce same rewritten query", q1, q3); checkBooleanQueryBoosts((BooleanQuery) q1); checkBooleanQueryBoosts((BooleanQuery) q2); + checkBooleanQueryBoosts((BooleanQuery) q3); } public void testBoosts() throws Exception { @@ -183,4 +198,39 @@ public class TestMultiTermQueryRewrites extends LuceneTestCase { checkBoosts(new MultiTermQuery.TopTermsScoringBooleanQueryRewrite(1024)); } + private void checkMaxClauseLimitation(MultiTermQuery.RewriteMethod method) throws Exception { + // default gets restored automatically by LuceneTestCase: + BooleanQuery.setMaxClauseCount(3); + + final MultiTermQuery mtq = new TermRangeQuery("data", "2", "7", true, true); + mtq.setRewriteMethod(method); + try { + multiSearcherDupls.rewrite(mtq); + fail("Should throw BooleanQuery.TooManyClauses"); + } catch (BooleanQuery.TooManyClauses e) { + // Maybe remove this assert in later versions, when internal API changes: + assertEquals("Should throw BooleanQuery.TooManyClauses with a stacktrace containing checkMaxClauseCount()", + "checkMaxClauseCount", e.getStackTrace()[0].getMethodName()); + } + } + + private void checkNoMaxClauseLimitation(MultiTermQuery.RewriteMethod method) throws Exception { + // default gets restored automatically by LuceneTestCase: + BooleanQuery.setMaxClauseCount(3); + + final MultiTermQuery mtq = new TermRangeQuery("data", "2", "7", true, true); + mtq.setRewriteMethod(method); + multiSearcherDupls.rewrite(mtq); + } + + public void testMaxClauseLimitations() throws Exception { + checkMaxClauseLimitation(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE); + checkMaxClauseLimitation(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE); + + checkNoMaxClauseLimitation(MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE); + checkNoMaxClauseLimitation(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT); + checkNoMaxClauseLimitation(new MultiTermQuery.TopTermsScoringBooleanQueryRewrite(1024)); + checkNoMaxClauseLimitation(new MultiTermQuery.TopTermsBoostOnlyBooleanQueryRewrite(1024)); + } + }