TopdocsString() public static method

public static TopdocsString ( Lucene.Net.Search.TopDocs docs, int start, int end ) : string
docs Lucene.Net.Search.TopDocs
start int
end int
return string
Beispiel #1
0
        private void AssertSubsetOfSameScores(Query q, TopDocs top1, TopDocs top2)
        {
            // The constrained query
            // should be a subset to the unconstrained query.
            if (top2.TotalHits > top1.TotalHits)
            {
                Assert.Fail("Constrained results not a subset:\n" + CheckHits.TopdocsString(top1, 0, 0) + CheckHits.TopdocsString(top2, 0, 0) + "for query:" + q.ToString());
            }

            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.AreEqual(score, otherScore, CheckHits.ExplainToleranceDelta(score, otherScore), "Doc " + id + " scores don't match\n" + CheckHits.TopdocsString(top1, 0, 0) + CheckHits.TopdocsString(top2, 0, 0) + "for query:" + q.ToString());
                    }
                }

                // check if subset
                if (!found)
                {
                    Assert.Fail("Doc " + id + " not found\n" + CheckHits.TopdocsString(top1, 0, 0) + CheckHits.TopdocsString(top2, 0, 0) + "for query:" + q.ToString());
                }
            }
        }
        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);
        }