Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Paul 184 posts 646 karma points
    Apr 12, 2019 @ 11:17
    Paul
    0

    Recursive Examine query; Return all pages where property or parent properties that contain a querystring parameter

    Hi,

    Edit Lets try again! Does anyone have an example of how I could go about creating an Examine query to get....

    All Child pages

    Where property value Category contains Querystring parameter, or any the Categories property parents contain the same Query String parameter?

    -- I was trying to do this using LINQ, which only got me the property and the properties parent property, I need at least 3 levels. e.g Home > Article page > Return Property value of category in article page > if it's got a parent category bring that back too > if that's got a parent bring that back too etc etc.

    -- Original Post below probably not that relevant now--

    I'm trying to pull back all the pages on my site that have a specific ID set in the package Utaxonomy and whilst I can get pages that have a tag easily, bringin back pages that contain parent tags is proving more challenging.

    In the below, QS is the GUID set within uTaxonomy.

    var AllPagesWithTags = Model.Content.Children.Where(x => x.IsVisible() && (x.GetPropertyValue<List<Taxonomy>>("category")
                                           .Select(y => y.Id.ToString()).ToList().Contains(qsCategory)
                                           ||
                                           x.GetPropertyValue<List<Taxonomy>>("category")
                                           .Select(y => y.Parent != null ? y.Parent.Id.ToString() : null).ToList().Contains(qsCategory))
                                           );
    

    Is there a way to write the above so it checks the parent, parents parent, parents parents parent etc, recursively so I can increase the number of taxonomy levels without having to amend the code?

  • Chris Norwood 131 posts 642 karma points
    Apr 12, 2019 @ 11:56
    Chris Norwood
    1

    I'm not sure about the package, but you could do:

    Model.Content.Descendants().Where(...)

    Which will search all children and children of children etc.

    There is obviously a performance hit to doing this though, especially on a large site.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Apr 14, 2019 @ 21:31
    Alex Skrypnyk
    0

    I would recommend avoiding using of Descendants(), read more - https://our.umbraco.com/documentation/Reference/Common-Pitfalls/#querying-with-descendants-descendantsorself

    The best way is to use XPath or examine indexes for such complex search.

  • Paul 184 posts 646 karma points
    Apr 15, 2019 @ 08:21
    Paul
    0

    Ok thanks guys.

    Yeah I wasn't going to use descendants, I only want to look at one level of child pages. So...

    Home > Article page > Property value of category in article page > if it's got a parent category bring that back > if that's got a parent bring that back etc etc.

    I thought Examine might be an option but was hoping to avoid it given I've very little experience with it.

  • Corné Strijkert 80 posts 456 karma points c-trib
    Apr 15, 2019 @ 12:39
    Corné Strijkert
    1

    If I understand it correctly you would like to get all the nodes where the parent category property is set to the querystring provided value.

    If so, my approach would be the following:

    Search with Examine for the nodes that contains the category value. Then instantiate the found pages with the Umbraco helper to get a list of IPublishedContent objects. Then you're able to retrieve the children of this pages.

    When you don't like it to use the Umbraco helper, I would add the parent category value to all child nodes in the Examine index using the OnGatheringNodeData event. When you are publishing a node that contains a category value you should publish all child nodes as well. Then in the OnGatheringNodeData event you can add custom fields to the Examine index. With this approach you can simple search for the category value on all nodes.

    Something like this:

    var helper = new UmbracoHelper(UmbracoContext.Current);
    var searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
    
    var query = searcher
         .CreateSearchCriteria(BooleanOperation.And)
         .Field("category", "")
         .Compile();
    
    var results = searcher.Search(query);
    
    IEnumerable<IPublishedContent> pages = results.SelectMany(x => helper.TypedContent(x.Id).Children);
    
  • Paul 184 posts 646 karma points
    Apr 15, 2019 @ 21:38
    Paul
    0

    Thank you, this looks great. Many thanks for taking the time to reply. :-)

  • Paul 184 posts 646 karma points
    Apr 23, 2019 @ 11:02
    Paul
    0

    Sorted with Examine. Thanks all.

Please Sign in or register to post replies

Write your reply to:

Draft