private void InitBlock(TestBoolean2 enclosingInstance) { this.enclosingInstance = enclosingInstance; }
// Random rnd is passed in so that the exact same random query may be created // more than once. public static BooleanQuery RandBoolQuery(System.Random rnd, int level, System.String field, System.String[] vals, TestBoolean2.Callback cb) { BooleanQuery current = new BooleanQuery(rnd.Next() < 0); for (int i = 0; i < rnd.Next(vals.Length) + 1; i++) { int qType = 0; // term query if (level > 0) { qType = rnd.Next(10); } Query q; if (qType < 7) q = new TermQuery(new Term(field, vals[rnd.Next(vals.Length)])); else q = RandBoolQuery(rnd, level - 1, field, vals, cb); int r = rnd.Next(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 AnonymousClassDefaultSimilarity(TestBoolean2 enclosingInstance) { InitBlock(enclosingInstance); }
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 DefaultSimilarityAnonymousInnerClassHelper(TestBoolean2 outerInstance) { this.OuterInstance = outerInstance; }
public DefaultSimilarityAnonymousClass(TestBoolean2 outerInstance) { this.outerInstance = outerInstance; }
// Random rnd is passed in so that the exact same random query may be created // more than once. public static BooleanQuery RandBoolQuery(System.Random rnd, bool allowMust, int level, System.String field, System.String[] vals, TestBoolean2.Callback cb) { BooleanQuery current = new BooleanQuery(rnd.Next() < 0); for (int i = 0; i < rnd.Next(vals.Length) + 1; i++) { int qType = 0; // term query if (level > 0) { qType = rnd.Next(10); } Query q; if (qType < 3) { q = new TermQuery(new Term(field, vals[rnd.Next(vals.Length)])); } else if (qType < 7) { q = new WildcardQuery(new Term(field, "w*")); } else { q = RandBoolQuery(rnd, allowMust, level - 1, field, vals, cb); } int r = rnd.Next(10); Occur occur; if (r < 2) { occur = Occur.MUST_NOT; } else if (r < 5) { occur = allowMust ? Occur.MUST : Occur.SHOULD; } else { occur = Occur.SHOULD; } current.Add(q, occur); } if (cb != null) cb.PostCreate(current); return current; }