UmbracoExamine ParentID?

时间:2016-03-21 07:18:13

标签: c# umbraco7 examine

我目前正在使用UmbracoExamine来满足我项目的所有搜索需求,而且我正在试图弄清楚查询参数究竟是什么" .ParentId"确实

我希望我能用它来查找来自parentID的所有子节点,但我似乎无法让它工作。

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

提前谢谢!

1 个答案:

答案 0 :(得分:1)

当你说它应该找到所有"那个类别""文章我假设你有一个类似下面的结构?

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

如果是这样,那么我也假设你想要所有的文章"编程"列出而不只是那些包含" 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")
        {

        }
    }