Exclude clause
The search filter can be build to exclude certain items by using a exclude tag clause.
Following code sample shows getting a list of products in the product catalog module through search. It has following search filtering:
- Search all products
- Which are published
- which has a particular product group id
- Excluding all products which has a value "Hardcover" for the property field named "SamplePrintFormat"
/// <summary>
/// Gets the published products except all products which has hardcover as the value in SamplePrintFormat property field.
/// </summary>
/// <param name="languageId">The language id.</param>
/// <param name="productGroupId">The product group id.</param>
/// <returns></returns>
private System.Collections.Generic.List<Product> GetPublishedProductsExcept(Guid languageId, Guid productGroupId)
{
//find all products which has a common parent of "productGroupId" above.
var request = new QueryRequest(languageId, ProductCatalogSearchDomains.Products, FoundationContext.Token);
//only published products.
request.FilterTags.Add(new Tag(TagNames.IsPublished, true));
//parent should be this product group.
request.FilterTags.Add(new Tag(TagNames.ProductGroupTreeId, productGroupId));
//exclude all Hardcover books.
//printformat tag name for "SamplePrintFormat" property
var printFormat = TagNames.GetTagNameForProperty("SamplePrintFormat");
request.ExcludeTags.Add(new Tag(printFormat, "Hardcover"));
var response = Solution.Instance.SearchService.Search(request);
var productList = Product.GetProducts(response.Hits.Select(x => new Guid(x.Id)));
return productList;
}