Skip to main content

Using Smart Search Filter via API/Code behind in Kentico

Introduction

This is sample how to use kentico smart search API + Smart Search Filtering via API / Code behind. The goal of this post is to show how you can use Smart Search API stated in this documentation. 

Please understand on how smart search syntax work before you proceed. You can read about this in kentico documentation below : 

https://docs.kentico.com/k9/configuring-kentico/setting-up-search-on-your-website/smart-search-syntax

   /// <summary>  
   /// Smart Search + Filtering. Change return type to suite your requirement  
   /// </summary>  
   /// <param name="filterValue">  
   /// Filtering Syntax  
   /// eg : +(Collections:(719c06e0-ccd2-436d-84f7-ef879849ca7e)) +(weddings:(4))  
   /// Collections and weddings are fields name  
   /// </param>  
   private void SmartSearchAPI(string filterValue)  
   {  
     int count = 0;  
     // Gets the search index  
     SearchIndexInfo index = SearchIndexInfoProvider.GetSearchIndexInfo("<index code>");  
     if (index != null)  
     {  
       var condition = new SearchCondition(filterValue, SearchModeEnum.AnyWord, SearchOptionsEnum.FullSearch, null, false);    
       string searchText = SearchSyntaxHelper.CombineSearchCondition("", condition);  
       // Prepares the search parameters  
       SearchParameters parameters = new SearchParameters()  
       {  
         SearchFor = searchText,  
         SearchSort = "##SCORE##",  
         Path = "/%",  
         ClassNames = "",  
         CurrentCulture = CMS.Localization.LocalizationContext.GetCurrentCulture().CultureCode.ToUpper(),  
         DefaultCulture = CultureHelper.EnglishCulture.IetfLanguageTag,  
         CombineWithDefaultCulture = false,  
         CheckPermissions = false,  
         SearchInAttachments = false,  
         User = (UserInfo)MembershipContext.AuthenticatedUser,  
         SearchIndexes = index.IndexName,  
         StartingPosition = 0,  
         DisplayResults = 100,  
         NumberOfProcessedResults = 100,  
         NumberOfResults = 0,  
         AttachmentWhere = String.Empty,  
         AttachmentOrderBy = String.Empty,  
       };  
       DataSet data = SearchHelper.Search(parameters);  
       if (parameters.NumberOfResults > 0)  
       {  
         count = parameters.NumberOfResults;  
       }  
     }  
   }  


Comments