Lucene.net 搜索引擎技術 及分頁

http://www.liveq.net/?p=142  詳細使用技術
1.首先得把Lucene.Net.dll這個添加引用;
2.Highlighter.Net.dll 可選,用於查詢字體高亮度顯示;
    
        /// 建立索引
        public void CreateIndex()
        {
            string indexPath = Server.MapPath("index");//定義索引文件存放路徑
            IndexWriter writer = new IndexWriter(indexPath, new StandardAnalyzer(), true);
            //第一個參數是路徑
            //第二個是索引分詞器,不同的分詞器代表不同的存放方式,目前分詞仍沒有達到100%標準
            //第三個參數是是否覆蓋已存在的索引(不覆蓋即以追加的形式寫入文件)
            DataTable dt = dal.GetList("");     // 從數據庫查詢集合列表,然後循環添加到Document文檔中去
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Document doc = new Document();//初始化索引文檔,就像一張白紙
                doc.Add(new Field("CompanyName", dt.Rows[i]["CompanyName"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                doc.Add(new Field("Comid", dt.Rows[i]["Comid"].ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
                //向白紙寫入鍵值對,索引是以哈希算法(依我理解)對文件的鍵值對進行搜索
                //Field.Store.YES對索引字段進行存儲,也就是返回的結果有該字段,相反Field.Store.NO就是不存儲
                //Field.Index.TOKENIZED,對該字段進行分詞處理,類似TSQL的like %% ,UN_TOKENIZED就是不分詞,全詞匹配
                writer.AddDocument(doc);//將設計好的白紙文件放進索引文件裏
            }
            writer.Optimize();//優化索引
            writer.Close();
        }
// 綁定顯示值
private void bidn()
        {

   // 分頁
   int pageSize = 12 ;   // 每頁顯示多少條數據
           int currentPage =  1; // 動態獲取每點擊下頁的索引currentpageIndex
            string indexPath = Server.MapPath("index");//定義索引文件存放路徑
            string keyword = this.txtserach.Text;   // 搜索傳入的條件
            if (keyword != "")
            {
                IndexSearcher searcher = new IndexSearcher(indexPath);
                Analyzer analyzer = new StandardAnalyzer();
                QueryParser query = new QueryParser("CompanyName", analyzer);

                Query query1 = query.Parse(keyword);
                BooleanQuery boolQuery = new BooleanQuery();

                // 高亮度顯示
                Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter("<font color=\"red\">", "</font>"), new                                        QueryScorer(query1));//使用高亮關鍵字顯示替代關鍵字
                highlighter.SetTextFragmenter(new SimpleFragmenter(100));

                DateTime time = DateTime.Now;
                Hits hits = searcher.Search(query1);
                DateTime end = DateTime.Now;
                long ltime = end.Millisecond - time.Millisecond;
                this.txtcontents.Text = "<font size=2 >關於 <b><font color=blue>" + keyword + "</font><b/> 共搜索到<b><font color=red>" + hits.Length() + "</font></b>個相關結果.搜索耗時:<font color=red>" + ltime + "</font>毫秒.</font></br>";

                if (hits != null)
                {
int start = pageSize * (currentPage - 1); int end = currentPage * pageSize; end = hits.Length()< end ? hits.Length(): end;

                    DataTable mytab = new DataTable();
                    mytab.Columns.Add("Comid");
                    mytab.Columns.Add("CompanyName");
                    mytab.Clear();
            //  添加需要顯示的列表字段
                    for (int i = start; i < end ; i++)     // 不分頁情況下for(int i=0;i<hits.length();i++)
                    {
                        Document doc = hits.Doc(i);
                       DataRow   dr = mytab.NewRow();
                        dr[0] = doc.Get("Comid").ToString();
                        dr[1] = doc.Get("CompanyName").ToString();
                        mytab.Rows.Add(dr);
                    }

                    PagedDataSource pds = new PagedDataSource();
                    pds.DataSource = mytab.DefaultView;
                    this.AspNetPager1.RecordCount = pds.Count;
                    pds.AllowPaging = true;
                    pds.CurrentPageIndex = this.AspNetPager1.CurrentPageIndex - 1;
                    pds.PageSize = 20;
                    this.rtshort.DataSource = pds;
                    this.rtshort.DataBind();
                }
            }

----- 第二種方法就是返回所有結果列表PagedDataSource 分頁
   PagedDataSource pds = new PagedDataSource();
                pds.DataSource = dtSearch.DefaultView;
               this.anpcurrent.RecordCount = pds.Count;
                pds.AllowPaging = true;
                pds.PageSize = this.anpcurrent.PageSize;
                pds.CurrentPageIndex = this.anpcurrent.CurrentPageIndex - 1;
                rtproduct.DataSource = pds;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章