admin/ManageSite.aspx.cslkbBuildIndex_Click()建立索引IndexManager.RebuildSafeIndex(30);lkbReBuildIndex_Click()重建索引IndexQueue que=new IndexQueue();Dottext.Framework.Util.ManagedThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(que.Run));aggsite/Search.ascx.csprotected 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; }