public static RandBoolQuery ( |
||
rnd | ||
allowMust | bool | |
level | int | |
field | string | |
vals | string | |
cb | Callback | |
return | Lucene.Net.Search.BooleanQuery |
public static BooleanQuery RandBoolQuery(Random rnd, bool allowMust, int level, string field, string[] vals, 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 < 4)
{
Term t1 = new Term(field, vals[rnd.Next(vals.Length)]);
Term t2 = new Term(field, vals[rnd.Next(vals.Length)]);
PhraseQuery pq = new PhraseQuery();
pq.Add(t1);
pq.Add(t2);
pq.Slop = 10; // increase possibility of matching
q = pq;
}
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);
BooleanClause.Occur occur;
if (r < 2)
{
occur = BooleanClause.Occur.MUST_NOT;
}
else if (r < 5)
{
if (allowMust)
{
occur = BooleanClause.Occur.MUST;
}
else
{
occur = BooleanClause.Occur.SHOULD;
}
}
else
{
occur = BooleanClause.Occur.SHOULD;
}
current.Add(q, occur);
}
if (cb != null)
{
cb.PostCreate(current);
}
return current;
}
}
public virtual void TestRandomQueries() { const string field = "data"; string[] vals = new 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 CallbackAnonymousInnerClassHelper(this, field, vals); // increase number of iterations for more complete testing int num = AtLeast(20); for (int i = 0; i < num; i++) { int lev = Random.Next(maxLev); int seed = Random.Next(); BooleanQuery q1 = TestBoolean2.RandBoolQuery(new Random(seed), true, lev, field, vals, null); // BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(seed), lev, field, vals, minNrCB); BooleanQuery q2 = TestBoolean2.RandBoolQuery(new Random(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); if (i < 100) { QueryUtils.Check( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, q1, s); QueryUtils.Check( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, q2, s); } AssertSubsetOfSameScores(q2, top1, top2); } // System.out.println("Total hits:"+tot); }