TestBoolean2public class TestBoolean2 extends TestCase Test BooleanQuery2 against BooleanQuery by overriding the standard query parser.
This also tests the scoring order of BooleanQuery. |
Fields Summary |
---|
private IndexSearcher | searcher | public static final String | field | private String[] | docFields |
Methods Summary |
---|
public org.apache.lucene.search.Query | makeQuery(java.lang.String queryText)
Query q = (new QueryParser(field, new WhitespaceAnalyzer())).parse(queryText);
return q;
| public void | queriesTest(java.lang.String queryText, int[] expDocNrs)
//System.out.println();
//System.out.println("Query: " + queryText);
try {
Query query1 = makeQuery(queryText);
BooleanQuery.setAllowDocsOutOfOrder(true);
Hits hits1 = searcher.search(query1);
Query query2 = makeQuery(queryText); // there should be no need to parse again...
BooleanQuery.setAllowDocsOutOfOrder(false);
Hits hits2 = searcher.search(query2);
CheckHits.checkHitsQuery(query2, hits1, hits2, expDocNrs);
} finally { // even when a test fails.
BooleanQuery.setAllowDocsOutOfOrder(false);
}
| public static org.apache.lucene.search.BooleanQuery | randBoolQuery(java.util.Random rnd, int level, java.lang.String field, java.lang.String[] vals, org.apache.lucene.search.TestBoolean2$Callback cb)
BooleanQuery current = new BooleanQuery(rnd.nextInt()<0);
for (int i=0; i<rnd.nextInt(vals.length)+1; i++) {
int qType=0; // term query
if (level>0) {
qType = rnd.nextInt(10);
}
Query q;
if (qType < 7) q = new TermQuery(new Term(field, vals[rnd.nextInt(vals.length)]));
else q = randBoolQuery(rnd, level-1, field, vals, cb);
int r = rnd.nextInt(10);
BooleanClause.Occur occur;
if (r<2) occur=BooleanClause.Occur.MUST_NOT;
else if (r<5) occur=BooleanClause.Occur.MUST;
else occur=BooleanClause.Occur.SHOULD;
current.add(q, occur);
}
if (cb!=null) cb.postCreate(current);
return current;
| public void | setUp()
RAMDirectory directory = new RAMDirectory();
IndexWriter writer= new IndexWriter(directory, new WhitespaceAnalyzer(), true);
for (int i = 0; i < docFields.length; i++) {
Document doc = new Document();
doc.add(new Field(field, docFields[i], Field.Store.NO, Field.Index.TOKENIZED));
writer.addDocument(doc);
}
writer.close();
searcher = new IndexSearcher(directory);
| public void | testQueries01()
String queryText = "+w3 +xx";
int[] expDocNrs = {2,3};
queriesTest(queryText, expDocNrs);
| public void | testQueries02()
String queryText = "+w3 xx";
int[] expDocNrs = {2,3,1,0};
queriesTest(queryText, expDocNrs);
| public void | testQueries03()
String queryText = "w3 xx";
int[] expDocNrs = {2,3,1,0};
queriesTest(queryText, expDocNrs);
| public void | testQueries04()
String queryText = "w3 -xx";
int[] expDocNrs = {1,0};
queriesTest(queryText, expDocNrs);
| public void | testQueries05()
String queryText = "+w3 -xx";
int[] expDocNrs = {1,0};
queriesTest(queryText, expDocNrs);
| public void | testQueries06()
String queryText = "+w3 -xx -w5";
int[] expDocNrs = {1};
queriesTest(queryText, expDocNrs);
| public void | testQueries07()
String queryText = "-w3 -xx -w5";
int[] expDocNrs = {};
queriesTest(queryText, expDocNrs);
| public void | testQueries08()
String queryText = "+w3 xx -w5";
int[] expDocNrs = {2,3,1};
queriesTest(queryText, expDocNrs);
| public void | testQueries09()
String queryText = "+w3 +xx +w2 zz";
int[] expDocNrs = {2, 3};
queriesTest(queryText, expDocNrs);
| public void | testQueries10()
String queryText = "+w3 +xx +w2 zz";
int[] expDocNrs = {2, 3};
searcher.setSimilarity(new DefaultSimilarity(){
public float coord(int overlap, int maxOverlap) {
return overlap / ((float)maxOverlap - 1);
}
});
queriesTest(queryText, expDocNrs);
| public void | testRandomQueries()
Random rnd = new Random(0);
String[] vals = {"w1","w2","w3","w4","w5","xx","yy","zzz"};
int tot=0;
try {
// increase number of iterations for more complete testing
for (int i=0; i<1000; i++) {
int level = rnd.nextInt(3);
BooleanQuery q1 = randBoolQuery(new Random(i), level, field, vals, null);
// Can't sort by relevance since floating point numbers may not quite
// match up.
Sort sort = Sort.INDEXORDER;
BooleanQuery.setAllowDocsOutOfOrder(false);
QueryUtils.check(q1,searcher);
Hits hits1 = searcher.search(q1,sort);
if (hits1.length()>0) hits1.id(hits1.length()-1);
BooleanQuery.setAllowDocsOutOfOrder(true);
Hits hits2 = searcher.search(q1,sort);
if (hits2.length()>0) hits2.id(hits1.length()-1);
tot+=hits2.length();
CheckHits.checkEqual(q1, hits1, hits2);
}
} finally { // even when a test fails.
BooleanQuery.setAllowDocsOutOfOrder(false);
}
// System.out.println("Total hits:"+tot);
|
|