When I last did this, I did so by creating a Custom Smart Search Module, which added the variant data to the index by implementing a method to handle the 'DocumentEvents.GetContent.Execute' event.
Example code below, but please note it does something special with a Color Option Category as I needed to have a limited list in the filter but an unlimited number of actual colors (ie. Scarlet, Red and Wine are available as variants but should all be filtered as Red).
/// <summary>
/// Custom module to include product options in search indexes.
/// </summary>
public class ProductSmartSearchModule : Module
{
// Module class constructor, the system registers the module under the name "CustomSmartSearch"
public ProductSmartSearchModule()
: base("ProductSmartSearchModule")
{
}
// Contains initialization code that is executed when the application starts
protected override void OnInit()
{
base.OnInit();
// Assigns a handler to the GetContent event for pages
DocumentEvents.GetContent.Execute += OnGetProductPageContent;
}
private void OnGetProductPageContent(object sender, DocumentSearchEventArgs e)
{
// Gets an object representing the page that is being indexed
TreeNode indexedPage = e.Node;
// Checks that the page exists and represents a product (SKU)
if (indexedPage != null && indexedPage.HasSKU)
{
// Gets the ID of the SKU
int skuId = indexedPage.NodeSKUID;
var categories = OptionCategoryInfoProvider.GetProductOptionCategories(skuId, true);
// Loops through the product option categories
foreach (var category in categories)
{
// Gets a list of enabled options in the product option category
var options = VariantHelper.GetEnabledOptionsWithVariantOptions(skuId, category.CategoryID);
string searchOptions = "";
if (options != null)
{
// Loops through the product options
foreach (var option in options)
{
// Adds the name of the product option into the indexed content for the product page
// Spaces added as separators to ensure that typical search index analyzers can correctly tokenize the index content
//e.Content += " " + option.SKUName + " ";
// handle special case if it is the color option category
if (category.CategoryID == SKUHelper.COLOR_OPTION_CATEGORY)
{
searchOptions += option.SKUDescription + " ";
}
else
{
searchOptions += option.SKUID + " ";
}
}
e.SearchDocument.Add("SKUOC_" + category.CategoryName, searchOptions, true, false);
}
}
}
}
}
(as a side note, perhaps someone can tell me how to set the code type when posting here so that it color codes correctly?)