How to add variant as a smart search filter in kentico

giridhar Addagalla asked on November 17, 2018 14:09

Hi Is there any possible to add variant as a smart search filter using web part or i should create a custom code to create a variant search filter with local index (with out azure).

Recent Answers


Suneel Jhangiani answered on November 17, 2018 14:47

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?)

0 votesVote for this answer Mark as a Correct answer

giridhar Addagalla answered on November 18, 2018 09:25

Actually i need to do this using the smart search filter using local indexs so is there any way to do like that, writing a search query in the smart search filter and showing the content in the dropdown .

0 votesVote for this answer Mark as a Correct answer

Suneel Jhangiani answered on November 19, 2018 11:40

That code adds the variant data to the Smart Search Index so that you can then access it using a smart search filter. You would still need to create a smart search filter that provides the UI for the user and sets the search web part filter condition.

Laura has an article on adding the data to the Index https://www.refactoredmedia.com/blog/november/kentico-custom-search-index-tips

The following is the Kentico docs for adding filters to your local searches https://docs.kentico.com/k11/configuring-kentico/setting-up-search-on-your-website/using-locally-stored-search-indexes/adding-filters-for-local-search-indexes

You basically need to do something like the below in your Filter web part:

ISearchFilterable searchWebpart = (ISearchFilterable) CMSControlsHelper.GetFilter(SearchWebpartID);
if (searchWebpart != null)
{
    searchWebpart.ApplyFilter(applyFilter, null, filterPostback);
}

Where 'applyFilter' is a string containing the Lucene filter text.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.