public static SearchResultDataSet <Article> Search(ArticleQuery query) { //触发事件 GlobalEvents.UserSearch(query.Title); //索引文件不存在时,返回null if (!GlobalSettings.CheckFileExist(PhysicalIndexDirectory)) { return(new SearchResultDataSet <Article>()); } DateTime startTime = DateTime.Now; BooleanQuery currentQuery = new BooleanQuery(); //CategoryID if (query.CategoryID.HasValue && query.CategoryID.Value != 0) { Term categoryIDTearm = new Term(NewsIndexField.CategoryID, query.CategoryID.ToString()); Query categoryIDQuery = new TermQuery(categoryIDTearm); currentQuery.Add(categoryIDQuery, BooleanClause.Occur.MUST); } //KeyWord if (!string.IsNullOrEmpty(query.Title)) { query.Title = SearchHelper.LuceneKeywordsScrubber(query.Title); if (!string.IsNullOrEmpty(query.Title)) { string[] searchFieldsForKeyword = new string[4]; searchFieldsForKeyword[0] = NewsIndexField.Title; searchFieldsForKeyword[1] = NewsIndexField.SubTitle; searchFieldsForKeyword[2] = NewsIndexField.Abstract; searchFieldsForKeyword[3] = NewsIndexField.Keywords; MultiFieldQueryParser articleWordQueryParser = new MultiFieldQueryParser(searchFieldsForKeyword, SearchHelper.GetChineseAnalyzer()); articleWordQueryParser.SetLowercaseExpandedTerms(true); articleWordQueryParser.SetDefaultOperator(QueryParser.OR_OPERATOR); string keyWordsSplit = SearchHelper.SplitKeywordsBySpace(query.Title); Query articleWordQuery = articleWordQueryParser.Parse(keyWordsSplit); currentQuery.Add(articleWordQuery, BooleanClause.Occur.MUST); } } //Search IndexSearcher searcher = new IndexSearcher(PhysicalIndexDirectory); Hits hits = searcher.Search(currentQuery); SearchResultDataSet <Article> articles = new SearchResultDataSet <Article>(); int pageLowerBound = query.PageIndex * query.PageSize; int pageUpperBound = pageLowerBound + query.PageSize; if (pageUpperBound > hits.Length()) { pageUpperBound = hits.Length(); } //HighLight PanGu.HighLight.Highlighter highlighter = null; if (!string.IsNullOrEmpty(query.Title)) { highlighter = new PanGu.HighLight.Highlighter(new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"#c60a00\">", "</font>"), new PanGu.Segment()); highlighter.FragmentSize = 100; } for (int i = pageLowerBound; i < pageUpperBound; i++) { Article item = ConvertDocumentToArticle(hits.Doc(i)); if (!string.IsNullOrEmpty(query.Title)) { string bestBody = null; if (!string.IsNullOrEmpty(item.Abstract) && item.Abstract.Length > MaxNumFragmentsRequired) { bestBody = highlighter.GetBestFragment(query.Title, item.Abstract); } if (!string.IsNullOrEmpty(bestBody)) { item.Abstract = bestBody; } else { item.Abstract = HtmlHelper.TrimHtml(item.Abstract, 100); } string bestSubject = null; if (!string.IsNullOrEmpty(item.Title) && item.Title.Length > MaxNumFragmentsRequired) { bestSubject = highlighter.GetBestFragment(query.Title, item.Title); } if (!string.IsNullOrEmpty(bestSubject)) { item.Title = bestSubject; } } articles.Records.Add(item); } searcher.Close(); articles.TotalRecords = hits.Length(); DateTime endTime = DateTime.Now; articles.SearchDuration = (endTime.Ticks - startTime.Ticks) / 1E7f; articles.PageIndex = query.PageIndex; articles.PageSize = query.PageSize; return(articles); }