Custom Search Provider

Kentico CMS allows you to write your own search provider that can use some third-party search engine. You can also write a custom search provider when you need to modify or filter search results returned by the standard search engine.

 

The following example explains how you can modify the standard search results so that they display the document path in the results in format

"> News > News 1"

instead of

"/News/News 1"

 

Tip: This is an example. If you only need do modify the search results format, you can do that by modifying the transformation searchresults in Site Manager -> Development -> Document Types -> edit Root -> Transformations.

 

1.Copy the CustomSearchProvider project from Kentico CMS installation (typically C:\Program Files\KenticoCMS\2.x\CodeSamples\CustomSearchProvider to some development folder (not under the web project).
2.Open the CMS web project using the WebProject.sln file. Click File -> Add -> Existing Project and select the CustomSearchProvider.csproj file in the folder where you copied the CustomSearchProvider project. Your Solution Explorer window will look like this:

 

clip0762

 

3.Unfold the References section of the CustomEventHandler project and delete invalid references.

 

4.Right-click the CustomSearchProvider project and choose Add Reference. Choose the Browse tab and locate the bin folder of your CMS web project on the disk. Choose to add a reference to the following libraries:
- CMS.ISearchEngine.dll
- CMS.SearchProviderSQL.dll
- CMS.DataEngine.dll

 

5.Unfold the bin folder in the CMS web project. Remove the CMS.CustomSearchProvider.dll reference. Right-click the bin folder and choose Add Reference. Choose the Projects tab and click OK to add the CustomSearchProvider project reference:
 
clip0763

 

 

6.Now you can modify the CustomSearchProvider library and place your code to the Search method. Enter the following code into the SearchProvider.Search method instead of the sample code:

 

[C#]

 

CMS.SearchProviderSQL.SearchProvider standardSearchProvider = new CMS.SearchProviderSQL.SearchProvider();

DataSet ds = standardSearchProvider.Search(siteName, searchNodePath, cultureCode, searchExpression,

               searchMode, searchChildNodes, classNames, filterResultsByReadPermission, searchOnlyPublished,

               whereCondition, orderBy);

          

if (ds.Tables.Count > 0)

{

  foreach (DataRow dr in ds.Tables[0].Rows)

  {

     dr["DocumentNamePath"] = ((string) dr["DocumentNamePath"]).Replace("/", " > ");

  }

}

return ds;

 
 

7.Set the following value in your web.config file:
 

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

 

 

8.Click Build -> Rebuild solution. Go to the live site and try to search for some text. You will see the search results in the following format:

 

clip0764