Copied to clipboard

Flag this post as spam?

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


  • Andrew Cullen 137 posts 237 karma points
    May 18, 2017 @ 22:52
    Andrew Cullen
    0

    Using two tag groups for one search

    I have one document type that has two different custom tag fields. What is the most efficient way to create a single search request that can query both fields at once? Should I try using TagQuery or go a more traditional route?

    EDIT: by "both fields at once", I mean one search field for each field, so I can search for docs with the value "a" in tag group 1 and "b" in tag group 2.

  • Craig Mayers 164 posts 508 karma points
    May 19, 2017 @ 10:45
    Craig Mayers
    2

    Hi Andrew,

    You could use TagQuery...

    For example:

    var helper = new UmbracoHelper(UmbracoContext.Current);
    var tags1 = helper.TagQuery.GetTagsForEntity(CurrentPage.Id, "TagGroup1NameHere");
    var tags2 = helper.TagQuery.GetTagsForEntity(CurrentPage.Id, "TagGroup2NameHere");
    
    List<Umbraco.Web.Models.TagModel> combinedResults = new List<Umbraco.Web.Models.TagModel>();
    combinedResults.AddRange(tags1);
    combinedResults.AddRange(tags2);
    

    Then you can do what you want with combinedResults (i.e. Loop over them OR use it as a data source for any data controls like the Repeater etc. - WebForms).

    Good luck!

    Craig

  • Andrew Cullen 137 posts 237 karma points
    May 19, 2017 @ 12:35
    Andrew Cullen
    0

    Thanks, Craig!

    Couple things, though - GetTagsForEntity seems like it pulls the tags for a single node. What I'm looking for is a search of all children node (of a certain docType). The combinedResults approach seems right, but I think what I need is Umbraco.TagQuery.GetContentByTag(tag, "TagGroup1NameHere").

    I didn't realize the groupName was a potential second parameter; thanks for pointing me in the right direction!

    Also, that addRange will create a superset of tags1 and tags2, while I need the intersection. What is the most effective way to pull the records common to both lists?

  • Craig Mayers 164 posts 508 karma points
    May 19, 2017 @ 12:55
    Craig Mayers
    0

    Hi Andrew,

    Yes, you are correct. .GetContentByTag is what you would want in your case.

    I would say the most effective way would be to loop over all results and convert them to a list of strongly typed objects (i.e. IPublishedContent OR you could create your own DTO) as the TagModel will only return you the Id of each node that it matched.

    Code example:

    IList<IPublishedContent> taggedNodes = new List<IPublishedContent>();
    foreach (var c in combinedResults)
    {
        var matchedNode = helper.TypedContent(c.Id);
        if (matchedNode != null)
        {
            taggedNodes.Add(matchedNode);
        }
    }
    

    You can then do what you wish with the strongly typed list taggedNodes

    Hope this helps ;)

    Regards

    Craig

Please Sign in or register to post replies

Write your reply to:

Draft