random boolean query testing

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@330900 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2005-11-04 21:15:02 +00:00
parent 9698aac274
commit 104aa2999e
2 changed files with 140 additions and 15 deletions

View File

@ -67,17 +67,77 @@ public class CheckHits {
checkDocIds("hits1", results, hits1); checkDocIds("hits1", results, hits1);
checkDocIds("hits2", results, hits2); checkDocIds("hits2", results, hits2);
checkEqual(query, hits1, hits2);
final float scoreTolerance = 1.0e-7f; }
for (int i = 0; i < results.length; i++) {
if (Math.abs(hits1.score(i) - hits2.score(i)) > scoreTolerance) { public static void checkEqual(Query query, Hits hits1, Hits hits2) throws IOException {
final float scoreTolerance = 1.0e-6f;
if (hits1.length() != hits2.length()) {
TestCase.fail("Unequal lengths: hits1="+hits1.length()+",hits2="+hits2.length());
}
for (int i = 0; i < hits1.length(); i++) {
if (hits1.id(i) != hits2.id(i)) {
TestCase.fail("Hit " + i + " docnumbers don't match\n"
+ hits2str(hits1, hits2,0,0)
+ "for query:" + query.toString());
}
if ((hits1.id(i) != hits2.id(i))
|| Math.abs(hits1.score(i) - hits2.score(i)) > scoreTolerance)
{
TestCase.fail("Hit " + i + ", doc nrs " + hits1.id(i) + " and " + hits2.id(i) TestCase.fail("Hit " + i + ", doc nrs " + hits1.id(i) + " and " + hits2.id(i)
+ "\nunequal scores: " + hits1.score(i) + "\nunequal : " + hits1.score(i)
+ "\n and: " + hits2.score(i) + "\n and: " + hits2.score(i)
+ "\nfor query:" + query.toString()); + "\nfor query:" + query.toString());
} }
} }
} }
public static String hits2str(Hits hits1, Hits hits2, int start, int end) throws IOException {
StringBuffer sb = new StringBuffer();
int len1=hits1==null ? 0 : hits1.length();
int len2=hits2==null ? 0 : hits2.length();
if (end<=0) {
end = Math.max(len1,len2);
}
sb.append("Hits length1=" + len1 + "\tlength2="+len2);
sb.append("\n");
for (int i=start; i<end; i++) {
sb.append("hit=" + i + ":");
if (i<len1) {
sb.append(" doc"+hits1.id(i) + "=" + hits1.score(i));
} else {
sb.append(" ");
}
sb.append(",\t");
if (i<len2) {
sb.append(" doc"+hits2.id(i) + "=" + hits2.score(i));
}
sb.append("\n");
}
return sb.toString();
}
public static String topdocsString(TopDocs docs, int start, int end) {
StringBuffer sb = new StringBuffer();
sb.append("TopDocs totalHits="+docs.totalHits + " top="+docs.scoreDocs.length+"\n");
if (end<=0) end=docs.scoreDocs.length;
else end=Math.min(end,docs.scoreDocs.length);
for (int i=start; i<end; i++) {
sb.append("\t");
sb.append(i);
sb.append(") doc=");
sb.append(docs.scoreDocs[i].doc);
sb.append("\tscore=");
sb.append(docs.scoreDocs[i].score);
sb.append("\n");
}
return sb.toString();
}
} }

View File

@ -20,6 +20,7 @@ package org.apache.lucene.search;
import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.analysis.WhitespaceAnalyzer; import org.apache.lucene.analysis.WhitespaceAnalyzer;
@ -31,6 +32,8 @@ import org.apache.lucene.queryParser.ParseException;
import junit.framework.TestCase; import junit.framework.TestCase;
import java.util.Random;
/** Test BooleanQuery2 against BooleanQuery by overriding the standard query parser. /** Test BooleanQuery2 against BooleanQuery by overriding the standard query parser.
* This also tests the scoring order of BooleanQuery. * This also tests the scoring order of BooleanQuery.
*/ */
@ -61,7 +64,7 @@ public class TestBoolean2 extends TestCase {
public Query makeQuery(String queryText) throws ParseException { public Query makeQuery(String queryText) throws ParseException {
return (new QueryParser(field, new WhitespaceAnalyzer())).parse(queryText); return (new QueryParser(field, new WhitespaceAnalyzer())).parse(queryText);
} }
public void queriesTest(String queryText, int[] expDocNrs) throws Exception { public void queriesTest(String queryText, int[] expDocNrs) throws Exception {
//System.out.println(); //System.out.println();
//System.out.println("Query: " + queryText); //System.out.println("Query: " + queryText);
@ -81,55 +84,55 @@ public class TestBoolean2 extends TestCase {
int[] expDocNrs = {2,3}; int[] expDocNrs = {2,3};
queriesTest(queryText, expDocNrs); queriesTest(queryText, expDocNrs);
} }
public void testQueries02() throws Exception { public void testQueries02() throws Exception {
String queryText = "+w3 xx"; String queryText = "+w3 xx";
int[] expDocNrs = {2,3,1,0}; int[] expDocNrs = {2,3,1,0};
queriesTest(queryText, expDocNrs); queriesTest(queryText, expDocNrs);
} }
public void testQueries03() throws Exception { public void testQueries03() throws Exception {
String queryText = "w3 xx"; String queryText = "w3 xx";
int[] expDocNrs = {2,3,1,0}; int[] expDocNrs = {2,3,1,0};
queriesTest(queryText, expDocNrs); queriesTest(queryText, expDocNrs);
} }
public void testQueries04() throws Exception { public void testQueries04() throws Exception {
String queryText = "w3 -xx"; String queryText = "w3 -xx";
int[] expDocNrs = {1,0}; int[] expDocNrs = {1,0};
queriesTest(queryText, expDocNrs); queriesTest(queryText, expDocNrs);
} }
public void testQueries05() throws Exception { public void testQueries05() throws Exception {
String queryText = "+w3 -xx"; String queryText = "+w3 -xx";
int[] expDocNrs = {1,0}; int[] expDocNrs = {1,0};
queriesTest(queryText, expDocNrs); queriesTest(queryText, expDocNrs);
} }
public void testQueries06() throws Exception { public void testQueries06() throws Exception {
String queryText = "+w3 -xx -w5"; String queryText = "+w3 -xx -w5";
int[] expDocNrs = {1}; int[] expDocNrs = {1};
queriesTest(queryText, expDocNrs); queriesTest(queryText, expDocNrs);
} }
public void testQueries07() throws Exception { public void testQueries07() throws Exception {
String queryText = "-w3 -xx -w5"; String queryText = "-w3 -xx -w5";
int[] expDocNrs = {}; int[] expDocNrs = {};
queriesTest(queryText, expDocNrs); queriesTest(queryText, expDocNrs);
} }
public void testQueries08() throws Exception { public void testQueries08() throws Exception {
String queryText = "+w3 xx -w5"; String queryText = "+w3 xx -w5";
int[] expDocNrs = {2,3,1}; int[] expDocNrs = {2,3,1};
queriesTest(queryText, expDocNrs); queriesTest(queryText, expDocNrs);
} }
public void testQueries09() throws Exception { public void testQueries09() throws Exception {
String queryText = "+w3 +xx +w2 zz"; String queryText = "+w3 +xx +w2 zz";
int[] expDocNrs = {2, 3}; int[] expDocNrs = {2, 3};
queriesTest(queryText, expDocNrs); queriesTest(queryText, expDocNrs);
} }
public void testQueries10() throws Exception { public void testQueries10() throws Exception {
String queryText = "+w3 +xx +w2 zz"; String queryText = "+w3 +xx +w2 zz";
int[] expDocNrs = {2, 3}; int[] expDocNrs = {2, 3};
@ -140,4 +143,66 @@ public class TestBoolean2 extends TestCase {
}); });
queriesTest(queryText, expDocNrs); queriesTest(queryText, expDocNrs);
} }
public void testRandomQueries() throws Exception {
Random rnd = new Random(0);
String[] vals = {"w1","w2","w3","w4","w5","xx","yy","zzz"};
int tot=0;
// 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.setUseScorer14(false);
Hits hits1 = searcher.search(q1,sort);
if (hits1.length()>0) hits1.id(hits1.length()-1);
BooleanQuery.setUseScorer14(true);
Hits hits2 = searcher.search(q1,sort);
if (hits2.length()>0) hits2.id(hits1.length()-1);
tot+=hits2.length();
CheckHits.checkEqual(q1, hits1, hits2);
}
// System.out.println("Total hits:"+tot);
}
// used to set properties or change every BooleanQuery
// generated from randBoolQuery.
public static interface Callback {
public void postCreate(BooleanQuery q);
}
// Random rnd is passed in so that the exact same random query may be created
// more than once.
public static BooleanQuery randBoolQuery(Random rnd, int level, String field, String[] vals, Callback cb) {
BooleanQuery current = new BooleanQuery();
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;
}
} }