Get TagTerms
Tag terms are the values the search index has indexed. It is easy to explain with an example: if we search for all articles who have value "Leo Tolstoy" for the property field "Author", then the search index "tag name" is "author property field" while its "value term" is "Leo Tolstoy". Observe that the "Author" is a property field in the article template, while "Leo Tolstoy" is a value given to this field in a particular article. In other words, for this particular example, "Leo Tolstoy" is a TagTerm for the search tag name "Author property field".
This article explains how to get these tag terms. For example, if we are interested in finding all the "authors" to produce a list of authors of the books we have.
Example
Following is a sample method which accepts a property field name as a parameter and returns a list of values for this property field.
/// <summary>
/// Gets the list of values in all articles for the given property name
/// </summary>
/// <param name="languageId">The language id.</param>
/// <param name="propertyName">Name of the property.</param>
/// <returns>list of values for the given property or an empty list</returns>
public static List<string> GetTagTerms(Guid languageId, string propertyName)
{
List<string> result = null;
var tagName = TagNames.GetTagNameForProperty(propertyName);
var allTerms = Solution.Instance.SearchService.GetTagTerms(ProductCatalogSearchDomains.Products, languageId, new[] { tagName });
if (allTerms != null)
{
var term = allTerms.FirstOrDefault();
if (term != null)
{
result = term.TermCounts.Select(x => x.Term).ToList();
}
}
return result ?? new List<string>();
}
Suppose the product catalog has the following articles, (where author is a text short searchable property for each article)
Then the above method will return the values for all authors: Leo Tolstoy, Charles Dickens, Alexandre Dumas, James G. Simmonds, Mary Jane Sterling.
Filtering the tag names
Suppose that we only want to find all authors who has written Mathematics books and only published products in the product catalog. In this case, the tag terms need to be filtered based on a search query. The technique is to pass a search result to the SearchService.GetTagTerms method that filters the data returned.
/// <summary>
/// Gets the list of values in articles belonging to given product group (recursively) for the given property name.
/// </summary>
/// <param name="languageId">The language id.</param>
/// <param name="productGroupId">The product group id.</param>
/// <param name="propertyName">Name of the property.</param>
/// <returns></returns>
public static List<string> GetTagTermsInProductGroup(Guid languageId, Guid productGroupId, string propertyName, SecurityToken token)
{
List<string> result = null;
var tagName = TagNames.GetTagNameForProperty(propertyName);
//search query to filter the tagterms result.
var request = new QueryRequest(languageId, ProductCatalogSearchDomains.Products, token);
//search for all products who has a parent with above product group id.
request.FilterTags.Add(new Tag(TagNames.ProductGroupTreeId, productGroupId));
//only included published products.
request.FilterTags.Add(new Tag(TagNames.IsPublished, bool.TrueString));
//get the search response and get tag terms in the response
var response = Solution.Instance.SearchService.Search(request);
var allTerms = Solution.Instance.SearchService.GetTagTerms(response, new[] { tagName }, false);
if (allTerms != null)
{
var term = allTerms.FirstOrDefault();
if (term != null)
{
result = term.TermCounts.Select(x => x.Term).ToList();
}
}
return result ?? new List<string>();
}