public virtual void TestRandomQueries() { System.Random rnd = NewRandom(); System.String field = "data"; System.String[] vals = new System.String[]{"1", "2", "3", "4", "5", "6", "A", "Z", "B", "Y", "Z", "X", "foo"}; int maxLev = 4; // callback object to set a random setMinimumNumberShouldMatch TestBoolean2.Callback minNrCB = new AnonymousClassCallback(rnd, this); // increase number of iterations for more complete testing for (int i = 0; i < 1000; i++) { int lev = rnd.Next(maxLev); long seed = rnd.Next(System.Int32.MaxValue); BooleanQuery q1 = TestBoolean2.RandBoolQuery(new System.Random((System.Int32) seed), true, lev, field, vals, null); // BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(seed), lev, field, vals, minNrCB); BooleanQuery q2 = TestBoolean2.RandBoolQuery(new System.Random((System.Int32) seed), true, lev, field, vals, null); // only set minimumNumberShouldMatch on the top level query since setting // at a lower level can change the score. minNrCB.PostCreate(q2); // Can't use Hits because normalized scores will mess things // up. The non-sorting version of search() that returns TopDocs // will not normalize scores. TopDocs top1 = s.Search(q1, null, 100); TopDocs top2 = s.Search(q2, null, 100); QueryUtils.Check(q1, s); QueryUtils.Check(q2, s); // The constrained query // should be a superset to the unconstrained query. Assert.IsFalse(top2.TotalHits > top1.TotalHits, "Constrained results not a subset:\n" + CheckHits.TopdocsString(top1, 0, 0) + CheckHits.TopdocsString(top2, 0, 0) + "for query:" + q2); for (int hit = 0; hit < top2.TotalHits; hit++) { int id = top2.ScoreDocs[hit].Doc; float score = top2.ScoreDocs[hit].Score; bool found = false; // find this doc in other hits for (int other = 0; other < top1.TotalHits; other++) { if (top1.ScoreDocs[other].Doc == id) { found = true; float otherScore = top1.ScoreDocs[other].Score; // check if scores match Assert.IsFalse(Math.Abs(otherScore - score) > 1.0e-6f, "Doc " + id + " scores don't match\n" + CheckHits.TopdocsString(top1, 0, 0) + CheckHits.TopdocsString(top2, 0, 0) + "for query:" + q2); } } // check if subset Assert.IsTrue(found, "Doc " + id + " not found\n" + CheckHits.TopdocsString(top1, 0, 0) + CheckHits.TopdocsString(top2, 0, 0) + "for query:" + q2); } } // System.out.println("Total hits:"+tot); }
public virtual void TestRandomQueries() { System.Random rnd = NewRandom(); System.String field = "data"; System.String[] vals = new System.String[] { "1", "2", "3", "4", "5", "6", "A", "Z", "B", "Y", "Z", "X", "foo" }; int maxLev = 4; // callback object to set a random setMinimumNumberShouldMatch TestBoolean2.Callback minNrCB = new AnonymousClassCallback(rnd, this); // increase number of iterations for more complete testing for (int i = 0; i < 1000; i++) { int lev = rnd.Next(maxLev); long seed = rnd.Next(System.Int32.MaxValue); BooleanQuery q1 = TestBoolean2.RandBoolQuery(new System.Random((System.Int32)seed), true, lev, field, vals, null); // BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(seed), lev, field, vals, minNrCB); BooleanQuery q2 = TestBoolean2.RandBoolQuery(new System.Random((System.Int32)seed), true, lev, field, vals, null); // only set minimumNumberShouldMatch on the top level query since setting // at a lower level can change the score. minNrCB.PostCreate(q2); // Can't use Hits because normalized scores will mess things // up. The non-sorting version of search() that returns TopDocs // will not normalize scores. TopDocs top1 = s.Search(q1, null, 100, null); TopDocs top2 = s.Search(q2, null, 100, null); QueryUtils.Check(q1, s); QueryUtils.Check(q2, s); // The constrained query // should be a superset to the unconstrained query. Assert.IsFalse(top2.TotalHits > top1.TotalHits, "Constrained results not a subset:\n" + CheckHits.TopdocsString(top1, 0, 0) + CheckHits.TopdocsString(top2, 0, 0) + "for query:" + q2); for (int hit = 0; hit < top2.TotalHits; hit++) { int id = top2.ScoreDocs[hit].Doc; float score = top2.ScoreDocs[hit].Score; bool found = false; // find this doc in other hits for (int other = 0; other < top1.TotalHits; other++) { if (top1.ScoreDocs[other].Doc == id) { found = true; float otherScore = top1.ScoreDocs[other].Score; // check if scores match Assert.IsFalse(Math.Abs(otherScore - score) > 1.0e-6f, "Doc " + id + " scores don't match\n" + CheckHits.TopdocsString(top1, 0, 0) + CheckHits.TopdocsString(top2, 0, 0) + "for query:" + q2); } } // check if subset Assert.IsTrue(found, "Doc " + id + " not found\n" + CheckHits.TopdocsString(top1, 0, 0) + CheckHits.TopdocsString(top2, 0, 0) + "for query:" + q2); } } // System.out.println("Total hits:"+tot); }