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
This commit is contained in:
Uwe Schindler 2010-11-15 10:15:39 +00:00
parent 6c4f3a10c5
commit ebad5443b2
2 changed files with 58 additions and 10 deletions

View File

@ -39,10 +39,8 @@ public class BooleanQuery extends Query implements Iterable<BooleanClause> {
* 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);
}
}

View File

@ -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));
}
}