Esempio n. 1
0
 private void  QueryTest(Query query)
 {
     ScoreDoc[] parallelHits = parallel.Search(query, null, 1000).scoreDocs;
     ScoreDoc[] singleHits   = single.Search(query, null, 1000).scoreDocs;
     Assert.AreEqual(parallelHits.Length, singleHits.Length);
     for (int i = 0; i < parallelHits.Length; i++)
     {
         Assert.AreEqual(parallelHits[i].score, singleHits[i].score, 0.001f);
         Document docParallel = parallel.Doc(parallelHits[i].doc);
         Document docSingle   = single.Doc(singleHits[i].doc);
         Assert.AreEqual(docParallel.Get("f1"), docSingle.Get("f1"));
         Assert.AreEqual(docParallel.Get("f2"), docSingle.Get("f2"));
         Assert.AreEqual(docParallel.Get("f3"), docSingle.Get("f3"));
         Assert.AreEqual(docParallel.Get("f4"), docSingle.Get("f4"));
     }
 }
Esempio n. 2
0
        /// <summary> This demonstrates a typical paging search scenario, where the search engine presents
        /// pages of size n to the user. The user can then go to the next page if interested in
        /// the next hits.
        ///
        /// When the query is executed for the first time, then only enough results are collected
        /// to fill 5 result pages. If the user wants to page beyond this limit, then the query
        /// is executed another time and all hits are collected.
        ///
        /// </summary>
        public static void  DoPagingSearch(System.IO.StreamReader in_Renamed, Searcher searcher, Query query, int hitsPerPage, bool raw, bool interactive)
        {
            // Collect enough docs to show 5 pages
            TopScoreDocCollector collector = TopScoreDocCollector.create(5 * hitsPerPage, false);

            searcher.Search(query, collector);
            ScoreDoc[] hits = collector.TopDocs().scoreDocs;

            int numTotalHits = collector.GetTotalHits();

            System.Console.Out.WriteLine(numTotalHits + " total matching documents");

            int start = 0;
            int end   = System.Math.Min(numTotalHits, hitsPerPage);

            while (true)
            {
                if (end > hits.Length)
                {
                    System.Console.Out.WriteLine("Only results 1 - " + hits.Length + " of " + numTotalHits + " total matching documents collected.");
                    System.Console.Out.WriteLine("Collect more (y/n) ?");
                    System.String line = in_Renamed.ReadLine();
                    if (line.Length == 0 || line[0] == 'n')
                    {
                        break;
                    }

                    collector = TopScoreDocCollector.create(numTotalHits, false);
                    searcher.Search(query, collector);
                    hits = collector.TopDocs().scoreDocs;
                }

                end = System.Math.Min(hits.Length, start + hitsPerPage);

                for (int i = start; i < end; i++)
                {
                    if (raw)
                    {
                        // output raw format
                        System.Console.Out.WriteLine("doc=" + hits[i].doc + " score=" + hits[i].score);
                        continue;
                    }

                    Document      doc  = searcher.Doc(hits[i].doc);
                    System.String path = doc.Get("path");
                    if (path != null)
                    {
                        System.Console.Out.WriteLine((i + 1) + ". " + path);
                        System.String title = doc.Get("title");
                        if (title != null)
                        {
                            System.Console.Out.WriteLine("   Title: " + doc.Get("title"));
                        }
                    }
                    else
                    {
                        System.Console.Out.WriteLine((i + 1) + ". " + "No path for this document");
                    }
                }

                if (!interactive)
                {
                    break;
                }

                if (numTotalHits >= end)
                {
                    bool quit = false;
                    while (true)
                    {
                        System.Console.Out.Write("Press ");
                        if (start - hitsPerPage >= 0)
                        {
                            System.Console.Out.Write("(p)revious page, ");
                        }
                        if (start + hitsPerPage < numTotalHits)
                        {
                            System.Console.Out.Write("(n)ext page, ");
                        }
                        System.Console.Out.WriteLine("(q)uit or enter number to jump to a page.");

                        System.String line = in_Renamed.ReadLine();
                        if (line.Length == 0 || line[0] == 'q')
                        {
                            quit = true;
                            break;
                        }
                        if (line[0] == 'p')
                        {
                            start = System.Math.Max(0, start - hitsPerPage);
                            break;
                        }
                        else if (line[0] == 'n')
                        {
                            if (start + hitsPerPage < numTotalHits)
                            {
                                start += hitsPerPage;
                            }
                            break;
                        }
                        else
                        {
                            int page = System.Int32.Parse(line);
                            if ((page - 1) * hitsPerPage < numTotalHits)
                            {
                                start = (page - 1) * hitsPerPage;
                                break;
                            }
                            else
                            {
                                System.Console.Out.WriteLine("No such page");
                            }
                        }
                    }
                    if (quit)
                    {
                        break;
                    }
                    end = System.Math.Min(numTotalHits, start + hitsPerPage);
                }
            }
        }
Esempio n. 3
0
        /// <summary> This demonstrates a typical paging search scenario, where the search engine presents 
        /// pages of size n to the user. The user can then go to the next page if interested in
        /// the next hits.
        /// 
        /// When the query is executed for the first time, then only enough results are collected
        /// to fill 5 result pages. If the user wants to page beyond this limit, then the query
        /// is executed another time and all hits are collected.
        /// 
        /// </summary>
        public static void DoPagingSearch(System.IO.StreamReader in_Renamed, Searcher searcher, Query query, int hitsPerPage, bool raw, bool interactive)
        {
            // Collect enough docs to show 5 pages
            TopScoreDocCollector collector = TopScoreDocCollector.create(5 * hitsPerPage, false);
            searcher.Search(query, collector);
            ScoreDoc[] hits = collector.TopDocs().scoreDocs;

            int numTotalHits = collector.GetTotalHits();
            System.Console.Out.WriteLine(numTotalHits + " total matching documents");

            int start = 0;
            int end = System.Math.Min(numTotalHits, hitsPerPage);

            while (true)
            {
                if (end > hits.Length)
                {
                    System.Console.Out.WriteLine("Only results 1 - " + hits.Length + " of " + numTotalHits + " total matching documents collected.");
                    System.Console.Out.WriteLine("Collect more (y/n) ?");
                    System.String line = in_Renamed.ReadLine();
                    if (line.Length == 0 || line[0] == 'n')
                    {
                        break;
                    }

                    collector = TopScoreDocCollector.create(numTotalHits, false);
                    searcher.Search(query, collector);
                    hits = collector.TopDocs().scoreDocs;
                }

                end = System.Math.Min(hits.Length, start + hitsPerPage);

                for (int i = start; i < end; i++)
                {
                    if (raw)
                    {
                        // output raw format
                        System.Console.Out.WriteLine("doc=" + hits[i].doc + " score=" + hits[i].score);
                        continue;
                    }

                    Document doc = searcher.Doc(hits[i].doc);
                    System.String path = doc.Get("path");
                    if (path != null)
                    {
                        System.Console.Out.WriteLine((i + 1) + ". " + path);
                        System.String title = doc.Get("title");
                        if (title != null)
                        {
                            System.Console.Out.WriteLine("   Title: " + doc.Get("title"));
                        }
                    }
                    else
                    {
                        System.Console.Out.WriteLine((i + 1) + ". " + "No path for this document");
                    }
                }

                if (!interactive)
                {
                    break;
                }

                if (numTotalHits >= end)
                {
                    bool quit = false;
                    while (true)
                    {
                        System.Console.Out.Write("Press ");
                        if (start - hitsPerPage >= 0)
                        {
                            System.Console.Out.Write("(p)revious page, ");
                        }
                        if (start + hitsPerPage < numTotalHits)
                        {
                            System.Console.Out.Write("(n)ext page, ");
                        }
                        System.Console.Out.WriteLine("(q)uit or enter number to jump to a page.");

                        System.String line = in_Renamed.ReadLine();
                        if (line.Length == 0 || line[0] == 'q')
                        {
                            quit = true;
                            break;
                        }
                        if (line[0] == 'p')
                        {
                            start = System.Math.Max(0, start - hitsPerPage);
                            break;
                        }
                        else if (line[0] == 'n')
                        {
                            if (start + hitsPerPage < numTotalHits)
                            {
                                start += hitsPerPage;
                            }
                            break;
                        }
                        else
                        {
                            int page = System.Int32.Parse(line);
                            if ((page - 1) * hitsPerPage < numTotalHits)
                            {
                                start = (page - 1) * hitsPerPage;
                                break;
                            }
                            else
                            {
                                System.Console.Out.WriteLine("No such page");
                            }
                        }
                    }
                    if (quit)
                        break;
                    end = System.Math.Min(numTotalHits, start + hitsPerPage);
                }
            }
        }