Search code examples
c#umbraco7examine

UmbracoExamine ParentID?


I'm currently using UmbracoExamine for all of my project's search needs, and I'm trying to figure out what exactly the query-parameter ".ParentId" does.

I was hoping I could use it to find all child nodes from a parentID, but I can't seem to get it working.

Basically, if the searchstring contains e.g. "C# Programming", it should find all that category's articles. This is just an example.

Thank you in advance!


Solution

  • When you say it should find all "that category's" articles I assume you have a structure like the below?

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

    If so then I assume as well that you want all the articles under "programming" to be listed and not just those containing "C# Programming"?

    What you will need to do is to loop through the SearchResults from your query and find the parent node from there

    IPublishedContent node = new UmbracoHelper(UmbracoContext.Current).TypedContent(item.Fields["id"].ToString());
    IPublishedContent parentNode = node.Parent;
    

    Once you have the parent node you can get all it's children as well as some of them depending on document type and what you want to do

    IEnumerable<IPublishedContent> allChildren = parentNode.Children;
    IEnumerable<IPublishedContent> specificChildren = parentNode.Children.Where(x => x.DocumentTypeAlias.Equals("aliasOfSomeDocType"));
    

    Example code below

        //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")
            {
    
            }
        }