コード例 #1
0
ファイル: PanGuAnalyzer.cs プロジェクト: yuyu2you/Caf.CMS
 public PanGuAnalyzer()
 {
     Analyzer = new Lucene.Net.Analysis.PanGu.PanGuAnalyzer();
 }
コード例 #2
0
        /// <summary>
        /// Does the search and stores the information about the results.
        /// </summary>
        private void search()
        {
            DateTime start = DateTime.Now;

            // create the searcher
            // index is placed in "index" subdirectory
            string indexDirectory = Server.MapPath("~/App_Data/index");

            //var analyzer = new StandardAnalyzer(Version.LUCENE_30);
            var analyzer = new Lucene.Net.Analysis.PanGu.PanGuAnalyzer();

            IndexSearcher searcher = new IndexSearcher(FSDirectory.Open(indexDirectory));

            // parse the query, "text" is the default field to search
            var parser = new QueryParser(Version.LUCENE_30, "name", analyzer);

            parser.AllowLeadingWildcard = true;

            Query query = parser.Parse(this.Query);

            // create the result DataTable
            this.Results.Columns.Add("title", typeof(string));
            this.Results.Columns.Add("sample", typeof(string));
            this.Results.Columns.Add("path", typeof(string));
            this.Results.Columns.Add("url", typeof(string));

            // search
            TopDocs hits = searcher.Search(query, 200);

            this.total = hits.TotalHits;

            // create highlighter
            IFormatter       formatter   = new SimpleHTMLFormatter("<span style=\"font-weight:bold;\">", "</span>");
            SimpleFragmenter fragmenter  = new SimpleFragmenter(80);
            QueryScorer      scorer      = new QueryScorer(query);
            Highlighter      highlighter = new Highlighter(formatter, scorer);

            highlighter.TextFragmenter = fragmenter;

            // initialize startAt
            this.startAt = InitStartAt();

            // how many items we should show - less than defined at the end of the results
            int resultsCount = Math.Min(total, this.maxResults + this.startAt);


            for (int i = startAt; i < resultsCount; i++)
            {
                // get the document from index
                Document doc = searcher.Doc(hits.ScoreDocs[i].Doc);

                TokenStream stream = analyzer.TokenStream("", new StringReader(doc.Get("name")));
                String      sample = highlighter.GetBestFragments(stream, doc.Get("name"), 2, "...");

                String path = doc.Get("path");

                // create a new row with the result data
                DataRow row = this.Results.NewRow();
                row["title"] = doc.Get("name");
                //row["path"] = "api/" + path;
                //row["url"] = "www.dotlucene.net/documentation/api/" + path;
                //row["sample"] = sample;

                this.Results.Rows.Add(row);
            }
            searcher.Dispose();

            // result information
            this.duration = DateTime.Now - start;
            this.fromItem = startAt + 1;
            this.toItem   = Math.Min(startAt + maxResults, total);
        }