You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-commits@lucene.apache.org by yo...@apache.org on 2005/11/04 22:15:13 UTC
svn commit: r330900 - in
/lucene/java/trunk/src/test/org/apache/lucene/search: CheckHits.java
TestBoolean2.java
Author: yonik
Date: Fri Nov 4 13:15:02 2005
New Revision: 330900
URL: http://svn.apache.org/viewcvs?rev=330900&view=rev
Log:
random boolean query testing
Modified:
lucene/java/trunk/src/test/org/apache/lucene/search/CheckHits.java
lucene/java/trunk/src/test/org/apache/lucene/search/TestBoolean2.java
Modified: lucene/java/trunk/src/test/org/apache/lucene/search/CheckHits.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/src/test/org/apache/lucene/search/CheckHits.java?rev=330900&r1=330899&r2=330900&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/CheckHits.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/CheckHits.java Fri Nov 4 13:15:02 2005
@@ -67,17 +67,77 @@
checkDocIds("hits1", results, hits1);
checkDocIds("hits2", results, 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) {
+ checkEqual(query, hits1, hits2);
+ }
+
+ 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)
- + "\nunequal scores: " + hits1.score(i)
+ + "\nunequal : " + hits1.score(i)
+ "\n and: " + hits2.score(i)
+ "\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();
+ }
+
}
Modified: lucene/java/trunk/src/test/org/apache/lucene/search/TestBoolean2.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/src/test/org/apache/lucene/search/TestBoolean2.java?rev=330900&r1=330899&r2=330900&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestBoolean2.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestBoolean2.java Fri Nov 4 13:15:02 2005
@@ -20,6 +20,7 @@
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.Term;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
@@ -31,6 +32,8 @@
import junit.framework.TestCase;
+import java.util.Random;
+
/** Test BooleanQuery2 against BooleanQuery by overriding the standard query parser.
* This also tests the scoring order of BooleanQuery.
*/
@@ -61,7 +64,7 @@
public Query makeQuery(String queryText) throws ParseException {
return (new QueryParser(field, new WhitespaceAnalyzer())).parse(queryText);
}
-
+
public void queriesTest(String queryText, int[] expDocNrs) throws Exception {
//System.out.println();
//System.out.println("Query: " + queryText);
@@ -81,55 +84,55 @@
int[] expDocNrs = {2,3};
queriesTest(queryText, expDocNrs);
}
-
+
public void testQueries02() throws Exception {
String queryText = "+w3 xx";
int[] expDocNrs = {2,3,1,0};
queriesTest(queryText, expDocNrs);
}
-
+
public void testQueries03() throws Exception {
String queryText = "w3 xx";
int[] expDocNrs = {2,3,1,0};
queriesTest(queryText, expDocNrs);
}
-
+
public void testQueries04() throws Exception {
String queryText = "w3 -xx";
int[] expDocNrs = {1,0};
queriesTest(queryText, expDocNrs);
}
-
+
public void testQueries05() throws Exception {
String queryText = "+w3 -xx";
int[] expDocNrs = {1,0};
queriesTest(queryText, expDocNrs);
}
-
+
public void testQueries06() throws Exception {
String queryText = "+w3 -xx -w5";
int[] expDocNrs = {1};
queriesTest(queryText, expDocNrs);
}
-
+
public void testQueries07() throws Exception {
String queryText = "-w3 -xx -w5";
int[] expDocNrs = {};
queriesTest(queryText, expDocNrs);
}
-
+
public void testQueries08() throws Exception {
String queryText = "+w3 xx -w5";
int[] expDocNrs = {2,3,1};
queriesTest(queryText, expDocNrs);
}
-
+
public void testQueries09() throws Exception {
String queryText = "+w3 +xx +w2 zz";
int[] expDocNrs = {2, 3};
queriesTest(queryText, expDocNrs);
}
-
+
public void testQueries10() throws Exception {
String queryText = "+w3 +xx +w2 zz";
int[] expDocNrs = {2, 3};
@@ -140,4 +143,66 @@
});
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;
+ }
+
+
}
Re: svn commit: r330900 - in /lucene/java/trunk/src/test/org/apache/lucene/search: CheckHits.java TestBoolean2.java
Posted by Paul Elschot <pa...@xs4all.nl>.
Yonik,
On Friday 04 November 2005 22:15, yonik@apache.org wrote:
> /** Test BooleanQuery2 against BooleanQuery by overriding the standard query parser.
I think you meant testing BooleanScorer2 against BooleanScorer ...
Thanks for extending TestBoolean2, regards,
Paul Elschot
---------------------------------------------------------------------
To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-dev-help@lucene.apache.org