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.
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).
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?
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
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.
Hi Andrew,
You could use TagQuery...
For example:
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
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?
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:
You can then do what you wish with the strongly typed list taggedNodes
Hope this helps ;)
Regards
Craig
is working on a reply...