我们在 Umbraco CMS 中创建了一个网站,我正在开发一个网站搜索功能。网站中有一个名为“新闻”的部分,定期发布更新。在列出新闻部分(以及其他页面)的结果时,我想在结果中更早地带来最新内容,例如,如果有人正在搜索“考试结果”,我想在创建页面之前带来 2018 年创建的匹配新闻页面2017年等等。如果有办法提升(查询时间或索引时间)以便提升最新页面?
以下是我到目前为止编写的代码:
var page = 1;
var pageSize = 5;
if (Request.QueryString["q"] != null)
searchQuery = Request.QueryString["q"];
if (Request.QueryString["page"] != null)
Int32.TryParse(Request.QueryString["page"], out page);
ISearchResults searchResults = null;
BaseSearchProvider searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
var headerFields = new[] { "contentTitle", "metaTags", "metaDescription", "nodeName" };
var contentFields = new[] { "contentDescription", "mainBody" };
var criteria = searcher.CreateSearchCriteria(IndexTypes.Content, BooleanOperation.Or);
var searchTerm = string.IsNullOrEmpty(Request["q"]) ? string.Empty : Request["q"];
if (searchTerm != string.Empty)
{
searchTerm = searchTerm.MakeSearchQuerySafe();
if (searchTerm.Length > 0)
{
searchTerm = searchTerm.Trim();
}
var examineQuery = criteria.GroupedOr(headerFields, searchTerm.Boost(100));
examineQuery.Or().GroupedOr(contentFields, searchTerm.Boost(50));
if (searchTerm.Contains(" "))
{
examineQuery.Or().GroupedOr(headerFields, searchTerm.RemoveStopWords().Split(' ').Select(x => x.MultipleCharacterWildcard().Value.Boost(10)).ToArray());
examineQuery.Or().GroupedOr(contentFields, searchTerm.RemoveStopWords().Split(' ').Select(x => x.MultipleCharacterWildcard()).ToArray());
}
searchResults = searcher.Search(examineQuery.Compile(), maxResults: pageSize * page);
}