首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UmbracoExamine ParentID?

UmbracoExamine ParentID?
EN

Stack Overflow用户
提问于 2016-03-21 07:18:13
回答 1查看 836关注 0票数 1

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

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

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

提前谢谢你!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-21 14:34:11

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

代码语言:javascript
复制
-- Programming
----Begin Java Programming
----Java Installation on Linux
----Basics of C# Programming
----What is SDLC
----Advanced C# Programming
-- Sports
----Baseball basics

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

您需要做的是从查询中循环遍历SearchResults,然后从那里找到父节点。

代码语言:javascript
复制
IPublishedContent node = new UmbracoHelper(UmbracoContext.Current).TypedContent(item.Fields["id"].ToString());
IPublishedContent parentNode = node.Parent;

一旦您有了父节点,您就可以获得所有的子节点以及一些子节点,这取决于文档类型和您想要做的事情

代码语言:javascript
复制
IEnumerable<IPublishedContent> allChildren = parentNode.Children;
IEnumerable<IPublishedContent> specificChildren = parentNode.Children.Where(x => x.DocumentTypeAlias.Equals("aliasOfSomeDocType"));

下面的示例代码

代码语言:javascript
复制
    //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")
        {

        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36125107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档