Dottext的lucene使用笔记

本文介绍了一个博客系统的索引建立与搜索功能实现细节,包括如何通过Lucene.NET创建文档索引、利用多线程进行索引重建、执行高效的数据绑定以及搜索查询优化等关键技术点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

admin/ManageSite.aspx.cs
lkbBuildIndex_Click()建立索引
IndexManager.RebuildSafeIndex(
30);
lkbReBuildIndex_Click()重建索引
IndexQueue que
=new IndexQueue();
Dottext.Framework.Util.ManagedThreadPool.QueueUserWorkItem(
new System.Threading.WaitCallback(que.Run));


aggsite
/Search.ascx.cs
protected System.Web.UI.WebControls.Repeater Results;
数据绑定
ResultSet searchResults 
= QueryIndex.SafeSearch(Config.CurrentBlog(Context).Application,SearchConfiguration.Blog,searchText,pageIndex,100);
searchResults 
= QueryIndex.SafeSearch(Request.Url.Host.Replace("www.",string.Empty),SearchConfiguration.Domain,searchText,pageIndex,100);
searchResults 
= QueryIndex.SafeSearch(searchText,pageIndex,100);
Results.DataSource 
= searchResults.Results;
   Results.DataBind(); 


建立索引
IndexManager.RebuildSafeIndex(
30):

Indexer index 
= new Indexer(path,false,isUseTempDirectory);
string sql = "blog_aggregate_Search";
string key="BuildIndex";
DateTime LastCompleted 
=DTOProvider.Instance().GetLastExecuteScheduledEventDateTime(key,Environment.MachineName);
SqlParameter[] p 
= {SqlHelper.MakeInParam("@StartDate",SqlDbType.DateTime,8,LastCompleted)};
DataTable table
= SqlHelper.ExecuteDataset(Config.Settings.BlogProviders.DbProvider.ConnectionString,CommandType.StoredProcedure,sql,p).Tables[0];
LastCompleted 
= DateTime.Now;
DTOProvider.Instance().SetLastExecuteScheduledEventDateTime(key,Environment.MachineName,LastCompleted);
for(int i=0;i<table.Rows.Count;i++)
{
    index.AddDocument(CreateDoc(table.Rows[i]));
}

table.Clear();
table.Dispose();
根据数据库的信息创建文档
Document,Field都是Lucene的类
using Lucene.Net.Documents;
private Document CreateDoc(DataRow reader)
{
    
//Null values are not allowed in the index
    
    Document doc 
= new Document();
    
try
    
{
        doc.Add(Field.Text(SearchConfiguration.Author,(
string)reader["Author"]));

        doc.Add(Field.Text(SearchConfiguration.Title,(
string)reader["Title"]));

        
string body = (string)reader["Text"];
        doc.Add(Field.UnIndexed(SearchConfiguration.Body,body));
        doc.Add(Field.Text(SearchConfiguration.Link,
string.Join(" ",GetLinks(body))));
        doc.Add(Field.Text(SearchConfiguration.RawPost,regexStripHTML.Replace(body,
string.Empty)));

        DateTime dateCreated 
= (DateTime)reader["DateAdded"];
        doc.Add(Field.UnIndexed(SearchConfiguration.DateCreated,dateCreated.ToLongDateString()));

        
string app = (string)reader["Application"];
        doc.Add(Field.Text(SearchConfiguration.Blog,app));

        
//Do we really need this?
        
//doc.Add(Field.Text(SearchConfiguration.Description,reader["Description"].ToString()));

        
string host = (string)reader["Host"];
        doc.Add(Field.Text(SearchConfiguration.Domain,host));


        
int posttype = (int)reader["PostType"];        
        doc.Add(Field.UnIndexed(SearchConfiguration.PostType,posttype.ToString()));

        
string permaLink  = null;
        
if((PostType)posttype == PostType.BlogPost)
        
{
            permaLink 
= string.Format(SearchConfiguration.Instance().UrlFormat,host,app, "archive/" + dateCreated.ToString("yyyy'/'MM'/'dd"),reader["EntryID"]);
        }

        
else if((PostType)posttype == PostType.Comment)
        
{
            permaLink 
=    reader["SourceUrl"].ToString()+"#"+reader["EntryID"].ToString();
        }

        
else
        
{
            permaLink 
= string.Format(SearchConfiguration.Instance().UrlFormat,host,app, "articles",reader["EntryID"]);
        }


        
int feedbackCount = (int)reader["FeedbackCount"];
        
int webviewCount = (int)reader["WebViewCount"];

        
int boost = weighter.Calculate(body.Length,feedbackCount,webviewCount,dateCreated,(PostType)posttype);
        doc.SetBoost(boost);

        doc.Add(Field.UnIndexed(SearchConfiguration.BoostFactor,boost.ToString()));

        doc.Add(Field.UnIndexed(SearchConfiguration.PermaLink,permaLink));
    }

    
catch(Exception e)
    
{
        Dottext.Framework.Logger.LogManager.Log(
"CreateDoc Fail","EntryID is "+reader["EntryID"]);
    }

    
    
return doc;
}


public void IndexQueue.Run(object state)                                                                           
{                                                                                                            
    IndexManager.RebuildSafeIndex();                                                                                                     
}
                     

QueryIndex.SafeSearch           
qi 
= new QueryIndex();
qi.FragementSize 
= fragmentSize;
return qi.Search(filterSearchText,filterField,searchText,pageIndex);                   
public ResultSet Search(string filterSearchText, string filterField, string searchText, int pageIndex)
{    
    
//Filter for a specific blog
    
    QueryFilter qf 
= new QueryFilter(QueryParser.Parse(filterSearchText,filterField,ConfigAnalyzer.GetAnalyzer()));
    
    
//How long does the search take
    StopWatch sw = new StopWatch();
    Query query 
= queryParser.Parse(FormatSearch(searchText));
    Hits hits 
= searcher.Search(query,qf);
    
long executionTime = sw.Peek();

    ResultSet results 
= GetResults(hits,pageIndex,query);
    results.ExecutionTime 
= executionTime;

    
    
return results;
}
             
IndexReader reader 
= IndexReader.Open(physicalPath);         
Searcher searcher 
= new IndexSearcher(reader);

[DllImport(
"kernel32.dll")]
extern static int QueryPerformanceCounter(ref long x);  
[DllImport(
"kernel32.dll")]
extern static int QueryPerformanceFrequency(ref long x);     
private long GetValue() {                                                                                                   
    
long ret = 0;                                                                                            
    
if (QueryPerformanceCounter(ref ret) == 0)                                                               
        
throw new NotSupportedException("Error while querying the high-resolution performance counter.");
    
return ret;                                                                                              
}
                                                                                                                
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值