Copied to clipboard

Flag this post as spam?

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


  • Chris Mahoney 233 posts 445 karma points
    Feb 23, 2014 @ 23:54
    Chris Mahoney
    0

    How can I implement "like"?

    Sorry if this has already been posted; "like" is a really difficult word to search for :)

    I'm running 6.1.6 and have created a Partial View Macro that produces a table based on child pages. I have a requirement that this table can be filtered by the user. At the moment I have code similar to this which allows the child pages' names to be filtered:

    var pages = CurrentPage.Children();
    var filteredPages = pages.Where("name == \"nameGoesHere\"");
    // do stuff with filteredPages.name, filteredPages.url etc.

    This works when the full page name has been entered, but I need to be able to handle partial names too. If it were SQL then I'd do something like this:

    var filteredPages = pages.Where("name LIKE \"%nameGoesHere%\"");

    This is the sort of thing that I'd think was really simple, but I'm having no luck finding any documentation on it (maybe it's so blindingly obvious that it wasn't worth documenting!)

    What's the easiest way to accomplish something like this?

    Thanks :)

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Feb 24, 2014 @ 20:47
    Jeavon Leopold
    0

    Hi Chris,

    I think maybe the search methods should do this, unfortunately I don't think they are documented yet but give them a try:

    E.g.

        var filteredDescendantPages = CurrentPage.AncestorOrSelf(1).SearchDescendants("links");
        var filteredChildPages = CurrentPage.AncestorOrSelf(1).SearchChildren("links");
    

    Jeavon

  • Chris Mahoney 233 posts 445 karma points
    Feb 25, 2014 @ 01:04
    Chris Mahoney
    0

    Thanks, that's got me on the right track, but is there any way to specify which fields should be searched?

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Feb 25, 2014 @ 13:06
    Jeavon Leopold
    0

    Hi Chris,

    I think you will need to construct a Examine ISearchCriteria but it does get a little complex, this might be a starting point though:

        var provider = ExamineManager.Instance.DefaultSearchProvider;
        var criteria = provider.CreateSearchCriteria(UmbracoExamine.IndexTypes.Content);
    
        var terms = new[] { "links" };
        var fields = new List<string> { "nodeName" };
    
        criteria.GroupedOr(fields, terms);
    
        var filteredChildPages = Model.Content.AncestorOrSelf(1).Search(criteria);
    

    Jeavon

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Feb 25, 2014 @ 15:08
    Jeavon Leopold
    100

    Hi Chris,

    I've been thinking about this and have improved the snippet to be a bit more useful?

        var propertyAlias = "title";
        var searchTerm = "links";
    
        var provider = ExamineManager.Instance.DefaultSearchProvider;
        var query = " +" + propertyAlias + ":" + searchTerm.MultipleCharacterWildcard().Value;                      
        var luceneQuery = "+parentID:" + Model.Content.AncestorOrSelf(1).Id + query;
        var criteria = provider.CreateSearchCriteria().RawQuery(luceneQuery);
        var filteredChildPages = Model.Content.AncestorOrSelf(1).Search(criteria);
    

    I actually think that the SearchChildren and SearchDescendants methods should have some overloads so you could do something like:

    var filteredChildPages = CurrentPage.AncestorOrSelf(1).SearchChildren("links", "myPropertyAlias");
    

    Or

    var filteredChildPages = CurrentPage.AncestorOrSelf(1).SearchChildren("links", new[] { "propertyAliasA", "propertyAliasB", "propertyAliasC" };);
    

    Jeavon

  • Chris Mahoney 233 posts 445 karma points
    Feb 26, 2014 @ 20:11
    Chris Mahoney
    0

    Thanks for that; unfortunately I've been dumped with some other work so it might be a few days before I can try it, but I'll let you know how I get on. Looks like I need to read about Examine! :)

  • Chris Mahoney 233 posts 445 karma points
    Feb 28, 2014 @ 00:39
    Chris Mahoney
    0

    I had some time today and got it working! Thanks so much!

    For the benefit of anyone else with a similar issue, here's what I ended up doing:

    String luceneString;

    luceneString += "+nodeName:" + Request.QueryString["name"] + " ";
    luceneString += "+reportID:" + Request.QueryString["reportID"] + " ";
    // etc.

    var provider = ExamineManager.Instance.DefaultSearchProvider;
    var luceneQuery = "+parentID:" + CurrentPage.Id + " " + luceneString;
    var criteria = provider.CreateSearchCriteria().RawQuery(luceneQuery);
    var pages = CurrentPage.Search(criteria);
Please Sign in or register to post replies

Write your reply to:

Draft