我目前正在使用 UmbracoExamine 来满足我项目的所有搜索需求,并且我正在尝试弄清楚查询参数“.ParentId”究竟是做什么的。
我希望我可以使用它从 parentID 中找到所有子节点,但我似乎无法让它工作。
基本上,如果搜索字符串包含例如“C# Programming”,它应该找到所有该类别的文章。这只是一个例子。
先感谢您!
当您说它应该找到所有“该类别的”文章时,我假设您具有如下结构?
-- Programming
----Begin Java Programming
----Java Installation on Linux
----Basics of C# Programming
----What is SDLC
----Advanced C# Programming
-- Sports
----Baseball basics
如果是这样,那么我还假设您希望列出“编程”下的所有文章,而不仅仅是那些包含“C# 编程”的文章?
您需要做的是遍历查询中的 SearchResults 并从那里找到父节点
IPublishedContent node = new UmbracoHelper(UmbracoContext.Current).TypedContent(item.Fields["id"].ToString());
IPublishedContent parentNode = node.Parent;
一旦你有了父节点,你就可以得到它的所有子节点以及其中的一些子节点,具体取决于文档类型和你想要做什么
IEnumerable<IPublishedContent> allChildren = parentNode.Children;
IEnumerable<IPublishedContent> specificChildren = parentNode.Children.Where(x => x.DocumentTypeAlias.Equals("aliasOfSomeDocType"));
下面的示例代码
//Fetching what eva searchterm some bloke is throwin' our way
string q = Request.QueryString["search"].Trim();
//Fetching our SearchProvider by giving it the name of our searchprovider
Examine.Providers.BaseSearchProvider Searcher = Examine.ExamineManager.Instance.SearchProviderCollection["SiteSearchSearcher"];
// control what fields are used for searching and the relevance
var searchCriteria = Searcher.CreateSearchCriteria(Examine.SearchCriteria.BooleanOperation.Or);
var query = searchCriteria.GroupedOr(new string[] { "nodeName", "introductionTitle", "paragraphOne", "leftContent", "..."}, q.Fuzzy()).Compile();
//Searching and ordering the result by score, and we only want to get the results that has a minimum of 0.05(scale is up to 1.)
IEnumerable<SearchResult> searchResults = Searcher.Search(query).OrderByDescending(x => x.Score).TakeWhile(x => x.Score > 0.05f);
//Printing the results
foreach (SearchResult item in searchResults)
{
//get the parent node
IPublishedContent node = new UmbracoHelper(UmbracoContext.Current).TypedContent(item.Fields["id"].ToString());
IPublishedContent parentNode = node.Parent;
//if you wish to check for a particular document type you can include this
if (item.Fields["nodeTypeAlias"] == "SubPage")
{
}
}