public virtual void  TestOutOfOrderCollection()
        {
            Directory   dir    = new RAMDirectory();
            IndexWriter writer = new IndexWriter(dir, null, MaxFieldLength.UNLIMITED);

            for (int i = 0; i < 10; i++)
            {
                writer.AddDocument(new Document());
            }
            writer.Commit();
            writer.Close();

            bool[]          inOrder         = new bool[] { false, true };
            System.String[] actualTSDCClass = new System.String[] { "OutOfOrderTopScoreDocCollector", "InOrderTopScoreDocCollector" };

            // Save the original value to set later.
            bool origVal = BooleanQuery.GetAllowDocsOutOfOrder();

            BooleanQuery.SetAllowDocsOutOfOrder(true);

            BooleanQuery bq = new BooleanQuery();

            // Add a Query with SHOULD, since bw.scorer() returns BooleanScorer2
            // which delegates to BS if there are no mandatory clauses.
            bq.Add(new MatchAllDocsQuery(), Occur.SHOULD);
            // Set minNrShouldMatch to 1 so that BQ will not optimize rewrite to return
            // the clause instead of BQ.
            bq.SetMinimumNumberShouldMatch(1);
            try
            {
                IndexSearcher searcher = new IndexSearcher(dir);
                for (int i = 0; i < inOrder.Length; i++)
                {
                    TopDocsCollector tdc = TopScoreDocCollector.create(3, inOrder[i]);
                    Assert.AreEqual("Lucene.Net.Search.TopScoreDocCollector+" + actualTSDCClass[i], tdc.GetType().FullName);

                    searcher.Search(new MatchAllDocsQuery(), tdc);

                    ScoreDoc[] sd = tdc.TopDocs().ScoreDocs;
                    Assert.AreEqual(3, sd.Length);
                    for (int j = 0; j < sd.Length; j++)
                    {
                        Assert.AreEqual(j, sd[j].doc, "expected doc Id " + j + " found " + sd[j].doc);
                    }
                }
            }
            finally
            {
                // Whatever happens, reset BooleanQuery.allowDocsOutOfOrder to the
                // original value. Don't set it to false in case the implementation in BQ
                // will change some day.
                BooleanQuery.SetAllowDocsOutOfOrder(origVal);
            }
        }
Beispiel #2
0
        public virtual void  TestRandomQueries()
        {
            System.Random rnd = NewRandom();

            System.String[] vals = new System.String[] { "w1", "w2", "w3", "w4", "w5", "xx", "yy", "zzz" };

            int tot = 0;

            BooleanQuery q1 = null;

            try
            {
                // increase number of iterations for more complete testing
                for (int i = 0; i < 1000; i++)
                {
                    int level = rnd.Next(3);
                    q1 = RandBoolQuery(new System.Random((System.Int32)rnd.Next(System.Int32.MaxValue)), level, field, vals, null);

                    // Can't sort by relevance since floating point numbers may not quite
                    // match up.
                    Sort sort = Sort.INDEXORDER;

                    BooleanQuery.SetAllowDocsOutOfOrder(false);

                    QueryUtils.Check(q1, searcher);

                    ScoreDoc[] hits1 = searcher.Search(q1, null, 1000, sort).scoreDocs;

                    BooleanQuery.SetAllowDocsOutOfOrder(true);
                    ScoreDoc[] hits2 = searcher.Search(q1, null, 1000, sort).scoreDocs;
                    tot += hits2.Length;
                    CheckHits.CheckEqual(q1, hits1, hits2);
                }
            }
            catch (System.Exception e)
            {
                // For easier debugging
                System.Console.Out.WriteLine("failed query: " + q1);
                throw e;
            }
            finally
            {
                // even when a test fails.
                BooleanQuery.SetAllowDocsOutOfOrder(false);
            }

            // System.out.println("Total hits:"+tot);
        }
Beispiel #3
0
        public virtual void  QueriesTest(System.String queryText, int[] expDocNrs)
        {
            //System.out.println();
            //System.out.println("Query: " + queryText);
            try
            {
                Query query1 = MakeQuery(queryText);
                BooleanQuery.SetAllowDocsOutOfOrder(true);
                ScoreDoc[] hits1 = searcher.Search(query1, null, 1000).scoreDocs;

                Query query2 = MakeQuery(queryText);                 // there should be no need to parse again...
                BooleanQuery.SetAllowDocsOutOfOrder(false);
                ScoreDoc[] hits2 = searcher.Search(query2, null, 1000).scoreDocs;

                CheckHits.CheckHitsQuery(query2, hits1, hits2, expDocNrs);
            }
            finally
            {
                // even when a test fails.
                BooleanQuery.SetAllowDocsOutOfOrder(false);
            }
        }