Creating custom SQL search providers

Kentico allows you to write your own SQL search provider, which you can use to integrate a third-party search engine. You can also create a custom search provider if you need to modify or filter search results returned by the standard search engine.

You cannot customize the search provider in the App_Code folder. The modifications must be added as part of a new assembly, which you then need to register via the CMSSearchProviderAssembly web.config key.

This provider only affects the SQL Search engine. The Smart Search engine remains unchanged.

To modify the smart search, create and use custom search indexes.

Adding the custom search provider assembly

The following example demonstrates how to customize the SQL search provider so that it only searches a limited section of the website.

  1. Copy the CustomSearchProvider project from your Kentico installation directory (typically C:\Program Files\Kentico\<version>\CodeSamples\CustomSearchProvider) to a development folder.

  2. Open your web project in Visual Studio (using the WebSite.sln/WebApp.sln file).

  3. Click File -> Add -> Existing Project and select CustomSearchProvider.csproj in the folder where you copied the CustomSearchProvider project.

  4. Unfold the References section of the CustomSearchProvider project and remove all invalid references.

  5. Right-click the CustomSearchProvider project and select Add Reference.

  6. Open the Browse tab of the Reference manager dialog, click Browse and navigate to the Lib folder of your Kentico web project.

  7. Add references to the following libraries:

    • CMS.Search.dll
    • CMS.SearchProviderSQL.dll
  8. Right‑click the main Kentico website object (or the CMSApp project if your installation is a web application) and select Add Reference.

  9. Open the Solution -> Projects tab and add a reference to the CustomSearchProvider project.

  10. Rebuild the CustomSearchProvider project.

The CustomSearchProvider project is now integrated into your application.

Modifying the search provider class

  1. Edit the SearchProvider.cs class in the CustomSearchProvider project.

  2. Modify the code of the Search method according to the following:

    
    
    
     public DataSet Search(string siteName, string searchNodePath, string cultureCode, string searchExpression, SearchModeEnum searchMode, bool searchChildNodes, string classNames, bool filterResultsByReadPermission, bool searchOnlyPublished, string whereCondition, string orderBy, bool combineWithDefaultCulture)
     {
         // Limits the search path to only the /Products section of the website
         searchNodePath = "/Products/%";
    
         // Gets a new instance of the SQL SearchProvider
         CMS.SearchProviderSQL.SearchProvider limitedSearchProvider = new CMS.SearchProviderSQL.SearchProvider();
    
         // Gets the search result dataset based on the parameters of the Search method and the limited search path
    
         DataSet searchResults = limitedSearchProvider.Search(siteName, searchNodePath, cultureCode, searchExpression, searchMode, searchChildNodes, classNames, filterResultsByReadPermission, searchOnlyPublished, whereCondition, orderBy, combineWithDefaultCulture);
    
         return searchResults;
     }
    
    
     
    1. Save the file and Rebuild the CustomSearchProvider project.

In real customization scenarios, you can modify the Search method to behave according to any requirements. The method must always return a DataSet containing the search results.

Registering the custom search provider

  1. Edit your application’s web.config file.

  2. Add the following key to the configuration/appSettings section:

    
    
    
     <add key="CMSSearchProviderAssembly" value="CMS.CustomSearchProvider" />
    
    
     

The system loads the SearchProvider class from the namespace specified as the value of the CMSSearchProviderAssembly key.

Result

To try out the customized search engine, open the live website and search for text using the SQL Search engine. For example, you can use the Examples -> Web Parts -> Full‑Text Search -> SQL Search -> SQL Search dialog with results page on the sample Corporate site.

The search now only returns results from the /Products section of the website.

This customization example is purely demonstrative. If you need to change the search path, directly set the Path property of individual SQL search web parts or controls.