Esempio n. 1
0
 public static Lucene.Net.Search.Hits search(Lucene.Net.Search.Query query)
 {
     Lucene.Net.Search.Hits hits = null;
     lock (my_lock) // prevent contention between searches and writing?
     {
         if (searcher == null)
         {
             searcher = new Lucene.Net.Search.IndexSearcher(MyLucene.index_path);
         }
         hits = searcher.Search(query);
     }
     return(hits);
 }
Esempio n. 2
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. 3
0
        public static void searchFor(Searcher searcher, string querystr)
        {
            QueryParser parser = new QueryParser("body", new StandardAnalyzer());    // could be outside this function
            Query query = parser.Parse(querystr);

            var hits = new AnonymousClassCollector();

            // more accurate timer
            var timer = new Stopwatch();
            timer.Start();
            searcher.Search(query, hits);
            timer.Stop();

            Console.WriteLine("search for [{0}] returned {1} hits in {2}ms )",
                query, hits.Count, timer.ElapsedMilliseconds);
        }
Esempio n. 4
0
		// LUCENE-1404
		private int HitCount(Searcher searcher, System.String word)
		{
			return searcher.Search(new TermQuery(new Term("text", word)), 10).totalHits;
		}
Esempio n. 5
0
 // LUCENE-1404
 private int HitCount(Searcher searcher, System.String word)
 {
     return(searcher.Search(new TermQuery(new Term("text", word)), 10).TotalHits);
 }
		public virtual void  DoSearching(Query unReWrittenQuery)
		{
			searcher = new IndexSearcher(ramDir);
			//for any multi-term queries to work (prefix, wildcard, range,fuzzy etc) you must use a rewritten query!
			query = unReWrittenQuery.Rewrite(reader);
			System.Console.Out.WriteLine("Searching for: " + query.ToString(FIELD_NAME));
			hits = searcher.Search(query);
		}
		public virtual void  TestUnRewrittenQuery()
		{
			//test to show how rewritten query can still be used
			searcher = new IndexSearcher(ramDir);
			Analyzer analyzer = new StandardAnalyzer();
			
			QueryParser parser = new QueryParser(FIELD_NAME, analyzer);
			Query query = parser.Parse("JF? or Kenned*");
			System.Console.Out.WriteLine("Searching with primitive query");
			//forget to set this and...
			//query=query.rewrite(reader);
			Hits hits = searcher.Search(query);
			
			//create an instance of the highlighter with the tags used to surround highlighted text
			//		QueryHighlightExtractor highlighter = new QueryHighlightExtractor(this, query, new StandardAnalyzer());
			Highlighter highlighter = new Highlighter(this, new QueryScorer(query));
			
			highlighter.SetTextFragmenter(new SimpleFragmenter(40));
			
			int maxNumFragmentsRequired = 3;
			
			for (int i = 0; i < hits.Length(); i++)
			{
				System.String text = hits.Doc(i).Get(FIELD_NAME);
				TokenStream tokenStream = analyzer.TokenStream(FIELD_NAME, new System.IO.StringReader(text));
				
				System.String highlightedText = highlighter.GetBestFragments(tokenStream, text, maxNumFragmentsRequired, "...");
				System.Console.Out.WriteLine(highlightedText);
			}
			//We expect to have zero highlights if the query is multi-terms and is not rewritten!
			Assert.IsTrue(numHighlights == 0, "Failed to find correct number of highlights " + numHighlights + " found");
		}
Esempio n. 8
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. 9
0
        /// <summary> This method uses a custom HitCollector implementation which simply prints out
        /// the docId and score of every matching document.
        ///
        /// This simulates the streaming search use case, where all hits are supposed to
        /// be processed, regardless of their relevance.
        /// </summary>
        public static void  DoStreamingSearch(Searcher searcher, Query query)
        {
            Collector streamingHitCollector = new AnonymousClassCollector();

            searcher.Search(query, streamingHitCollector);
        }
Esempio n. 10
0
        public SearchModel Search(string searchText)
        {
            var result = new SearchModel();

            if (string.IsNullOrEmpty(searchText))
            {
                result.Message = "Įveskite paieškos užklausą.";
                return(result);
            }

            var stemmedSearchText = new LithuanianStemmer().Stem(searchText.Trim());

            if (string.IsNullOrEmpty(stemmedSearchText))
            {
                result.Message = "Įveskite paieškos užklausą.";
                return(result);
            }

            Lucene.Net.Search.Hits hits = null;
            try
            {
                if (char.IsLetter(stemmedSearchText[stemmedSearchText.Length - 1]))
                {
                    stemmedSearchText += "*";
                }

                query = parser.Parse(stemmedSearchText);

                if (searcher == null)
                {
                    searcher = new Lucene.Net.Search.IndexSearcher(CustomAppSettings.SearchIndexFolder);
                }

                hits = searcher.Search(query);
            }
            catch (Exception e)
            {
                result.Message = "Paieška nepavyko. Pataisykite užklausą. Klaidos pranešimas: " + e.Message;
                return(result);
            }

            Lucene.Net.Highlight.Formatter formatter = new Lucene.Net.Highlight.SimpleHTMLFormatter(
                "<span class=\"highlightResult\">",
                "</span>");

            var fragmenter  = new Lucene.Net.Highlight.SimpleFragmenter(100);
            var scorer      = new Lucene.Net.Highlight.QueryScorer(searcher.Rewrite(query));
            var highlighter = new Lucene.Net.Highlight.Highlighter(formatter, scorer);

            highlighter.SetTextFragmenter(fragmenter);

            Dictionary <string, int> dict_already_seen_ids = new Dictionary <string, int>();

            var list = new List <SearchIndexModel>();

            // insert the search results into a temp table which we will join with what's in the database
            for (int i = 0; i < hits.Length(); i++)
            {
                if (dict_already_seen_ids.Count < 100)
                {
                    Lucene.Net.Documents.Document doc = hits.Doc(i);
                    string id = doc.Get("id");
                    if (!dict_already_seen_ids.ContainsKey(id))
                    {
                        dict_already_seen_ids[id] = 1;
                        var model = new SearchIndexModel();
                        model.Id      = id;
                        model.Score   = hits.Score(i);
                        model.Subject = doc.Get("subject");
                        model.Type    = (EntryTypes)Enum.Parse(typeof(EntryTypes), doc.Get("type"));

                        string raw_text = HttpUtility.HtmlEncode(doc.Get("raw_text"));
                        //string raw_text = doc.Get("raw_text");

                        Lucene.Net.Analysis.TokenStream stream = analyzer.TokenStream("text",
                                                                                      new System.IO.StringReader(
                                                                                          raw_text));
                        string highlighted_text = highlighter.GetBestFragments(stream, raw_text, 3, "...").Replace("'",
                                                                                                                   "''");


                        if (highlighted_text == "") // someties the highlighter fails to emit text...
                        {
                            highlighted_text = raw_text.Replace("'", "''");
                        }
                        if (highlighted_text.Length > 3000)
                        {
                            highlighted_text = highlighted_text.Substring(0, 3000);
                        }

                        model.HighlightedText = highlighted_text;

                        list.Add(model);
                    }
                }
                else
                {
                    break;
                }
            }

            result.List         = list;
            result.SearchPhrase = searchText;
            if (list.Count == 0)
            {
                result.Message = string.Format("Įrašų pagal užklausą '{0}' nerasta. Patikslinkite paieškos duomenis.", searchText);
            }

            return(result);
        }
Esempio n. 11
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. 12
0
        /// <summary> This method uses a custom HitCollector implementation which simply prints out
        /// the docId and score of every matching document. 
        /// 
        /// This simulates the streaming search use case, where all hits are supposed to
        /// be processed, regardless of their relevance.
        /// </summary>
        public static void DoStreamingSearch(Searcher searcher, Query query)
        {
            Collector streamingHitCollector = new AnonymousClassCollector();

            searcher.Search(query, streamingHitCollector);
        }