在 Config/ExamineIndex.config 中,您创建一个新的 IndexSet,其中包含您希望用户能够搜索的所有属性。
Umbraco.TypedSearch(Request.QueryString["query"]); 将在整个页面中搜索任何内容,但如果您正确设置 UmbracoExamine,您可以选择希望人们能够搜索的文档类型,以及您可以选择希望人们能够索引的属性类型基于的内容。
例如:
<IndexSet SetName="ExternalTopLevelSearchSet" IndexPath="~/App_Data/TEMP/ExamineIndexes/{machinename}/TopLevelSearch/">
<IndexAttributeFields>
<add Name="Name"/>
<add Name="bodyText"/>
<add Name="tags"/>
<add Name="themes"/>
<add Name="parentID"/>
</IndexAttributeFields>
<IncludeNodeTypes>
<add Name="ArticlePage" />
</IncludeNodeTypes>
</IndexSet>
然后你必须在 Config/ExamineSettings.config 中创建一个索引器
<add name="ExternalTopLevelIndexer" type="UmbracoExamine.UmbracoContentIndexer, UmbracoExamine"
supportUnpublished="false"
supportProtected="false"
interval="10"
analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net"
indexSet="ExternalTopLevelSearchSet"/>
现在,您也在 Config/ExamineSettings.config 中创建了一个搜索提供程序
<add name="ExternalTopLevelSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine"
analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net" indexSet="ExternalTopLevelSearchSet" enableLeadingWildcards="true"/>
这是我使用上面显示的“ExternalTopLevel”搜索器的 C# 代码。
public List<SearchResult> SearchResults
{
get
{
if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["q"])) {
var searcher = ExamineManager.Instance.SearchProviderCollection["ExternalTopLevelSearcher"];
// Search criteria.
var searchCriteria = searcher.CreateSearchCriteria(BooleanOperation.Or);
var q = HttpContext.Current.Request.QueryString["q"].ToLower().Trim().Split(' ');
var contentType = HelperClass.GetContentTypeNodes().FirstOrDefault(item => q.Contains(item.Name.ToLower()));
q = q.Where(i => i.Length > 3).ToArray();
var query = searchCriteria
.GroupedOr(new[] { "nodeName" }, q.Select(x => x.Boost(150)).ToArray())
.Or()
.GroupedOr(new[] { "grid" }, q.Select(x => x.Boost(80)).ToArray())
.Or()
.GroupedOr(new[] { "tags", "themes", "institutions" }, q.Select(x => x.Boost(110)).ToArray());
// Search results
var searchResults = searcher.Search(query.Compile()).OrderByDescending(x => x.Score);
return searchResults.ToList();
}
return new List<SearchResult>();
}
}
希望这可以帮助你!