lucene.net2.0 搜索

#region 建立索引
public void CreateIndex()
        {       
            DataSet ds
= 取得数据库信息;
           
//取得索引输出
            IndexWriter writer = new IndexWriter(这个地方写保存路径, true);
            writer.SetMergeFactor(
20);  //      调整segment合并的频率和大小
           
//建立索引字段
            if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
               
foreach (DataRow dr in ds.Tables[0].Rows)
                {
                   
//取得Doc对象
                    Document doc = GetDocumentObj(文档信息写在这里,根据实际情况可以包含标题,内容,时间etc);                 
                   
//写入索引文档
                    writer.AddDocument(doc);
                }
            }
           
//优化文档
            writer.Optimize();
           
//关闭
            writer.Close();
        }
       
#endregion

#region 更新索引
       
/// <summary>
       
/// 通过fileindex的fileid更新  这个地方由于2.0不像java的2.2有update方法,所以只能先删除后添加了
       
/// </summary>
       
/// <param name="fileID"></param>
        public void UpdateIndex(int fileID)
        {
           
//这里根据ID取得信息对象IdxObj;

           
if (fileIdxObj == null)     //无法查询,返回
                return;

           
//取得索引修改器
            IndexModifier modifier = new IndexModifier(路径, false);
           
//删除原索引
            modifier.DeleteDocuments(new Term(此处根据fileid查询));
           
//建立索引字段
            Document doc = GetDocumentObj(idxObj的信息);
           
//写入索引文档
            modifier.AddDocument(doc);
           
//优化
            modifier.Optimize();
           
//关闭
            modifier.Close();
        }       
       
#endregion

#region 添加索引
       
public void AddIndex(信息)
        { 
           
//取得索引修改器
            IndexModifier modifier = new IndexModifier(路径, new StandardAnalyzer(), false);           
           
//建立索引字段
            Document doc = GetDocumentObj(信息);
           
//写入索引文档
            modifier.AddDocument(doc);
           
//优化
            modifier.Optimize();
           
//关闭
            modifier.Close();

        }

       
#endregion

#region LuceneSearch
       
/// <summary>
       
/// 根据keyword检索数据,反回已分页的Dataset
       
/// </summary>
       
/// <param name="keyword">查找的关键字</param>
       
/// <param name="pageSize">分页每页的大小</param>
       
/// <param name="pageIndex">反回第几页的数据</param>
       
/// <returns>结果数据集,Tables[0]是结果数据,Tables[1]是存放总记录数(total数据)以及总页数的表</returns>
        public DataSet LuceneSearch(string keyword, Int32 pageSize, Int32 pageIndex)
        {           
           
//取得表结构
            DataTable mytab = GetDataTable();
           
int result = 0;     //结果
            int pageCount=0;    //页数
            int startPost = 0;
            DataSet ds;
            Query query;
            IndexSearcher mysea;
           
//取得搜索结果
            Hits myhit = seacher(keyword,out mysea, out query);
            Highlighter hl
= new Highlighter(new SimpleHTMLFormatter("<font class=\"Highlighter\">", "</font>"), new QueryScorer(query));
           
           
//设置高亮检索的范围 数值大=准确度高+速度慢
            hl.SetMaxDocBytesToAnalyze(int.MaxValue);

           
if (myhit != null)
            {
                result
= myhit.Length();
               
//计算分页信息
                cacuPageInfo(pageSize, ref pageIndex, result, ref pageCount, ref startPost);

               
for (int i = 0; i < pageSize && startPost < result; i++, startPost++)
                {
                   
//添加信息
                    AppendTableRow(keyword, mytab, myhit, hl, startPost);
                }               
               
//填充结果
                ds= FillResultDataSet(mytab, result, pageCount);               
            }
           
else
            {
                ds
= null;
            }
           
if(mysea!=null)
                mysea.Close();     
//关闭查询
            return ds;
        }

/// <summary>
       
/// 取得搜索信息
       
/// </summary>
       
/// <param name="queryString"></param>
       
/// <param name="query"></param>
       
/// <returns></returns>
        public Hits seacher(String queryString,out IndexSearcher mysea, out Query query )
        {
            Hits hits
= null;
           
try
            {
                mysea
= new IndexSearcher(路径);
                QueryParser qp
= new QueryParser(查询字段, new StandardAnalyzer());
                query
= qp.Parse(queryString);
                hits
= mysea.Search(query);
               
            }
           
catch (Exception e)
            {
                query
= null;
                mysea
= null;
            }
           
return hits;
        }
posted @ 2010-06-16 00:08  冰封的心  阅读(148)  评论(0编辑  收藏  举报