Umbraco 检查 ParentID?

UmbracoExamine ParentID?

我目前正在使用 UmbracoExamine 来满足我项目的所有搜索需求,并且我正在尝试弄清楚查询参数“.ParentId”到底做了什么。

我希望我可以使用它从 parentID 中找到所有子节点,但我似乎无法让它工作。

基本上,如果搜索字符串包含例如"C# Programming",它应该找到该类别的所有文章。这只是一个例子。

提前致谢!

当你说它应该找到所有 "that category's" 篇文章时,我假设你的结构如下所示?

-- Programming
----Begin Java Programming
----Java Installation on Linux
----Basics of C# Programming
----What is SDLC
----Advanced C# Programming
-- Sports
----Baseball basics

如果是这样,那么我也假设您希望列出 "programming" 下的所有文章,而不仅仅是包含 "C# Programming"?

的文章

您需要做的是循环查询查询的 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")
        {

        }
    }