LUCENE-5433: test of BooleanQuery.rewrite's single clause optimization

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1594330 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Chris M. Hostetter 2014-05-13 19:11:07 +00:00
parent be017318d9
commit f0001135dc
1 changed files with 38 additions and 0 deletions

View File

@ -352,4 +352,42 @@ public class TestBooleanQuery extends LuceneTestCase {
dir.close();
}
public void testOneClauseRewriteOptimization() throws Exception {
final float BOOST = 3.5F;
final String FIELD = "content";
final String VALUE = "foo";
Directory dir = newDirectory();
(new RandomIndexWriter(random(), dir)).shutdown();
IndexReader r = DirectoryReader.open(dir);
TermQuery expected = new TermQuery(new Term(FIELD, VALUE));
expected.setBoost(BOOST);
final int numLayers = atLeast(3);
boolean needBoost = true;
Query actual = new TermQuery(new Term(FIELD, VALUE));
for (int i = 0; i < numLayers; i++) {
if (needBoost && 0 == TestUtil.nextInt(random(),0,numLayers)) {
needBoost = false;
actual.setBoost(BOOST);
}
BooleanQuery bq = new BooleanQuery();
bq.add(actual, random().nextBoolean()
? BooleanClause.Occur.SHOULD : BooleanClause.Occur.MUST);
actual = bq;
}
if (needBoost) {
actual.setBoost(BOOST);
}
assertEquals(numLayers + ": " + actual.toString(),
expected, actual.rewrite(r));
r.close();
dir.close();
}
}