March 28, 2017 Development Tips

Show custom meta data in Kentico search results

By: Laura Frese


In this post I will show you how to add custom data into the smart search index and display that custom field as the search result details. Its a relatively quick change to make if you have access to the files on the server. 
 

First, follow the instructions provided in Kentico's documentation Customizing the content of search indexes under the example "Adding data from a user field to page indexes"
 

Then, replace the code block under if(indexedPage != null) as follows:


if (indexedPage != null)
{
    string customdetails = "My custom details"; //Run logic to get the proper custom details
    e.SearchDocument.AddGeneralField("CustomDetails", description, true, false);
}


Once that is complete you will need to rebuild your index.
 

Finally, use the transformation method in your search results transformation to get the value.

<%# GetSearchValue("CustomDetails") %>


Keep in mind that this transformation method can get any field already associated with the document, so if you want to just replace the provided description with the meta description, use

<%# GetSearchValue("DocumentPageDescription") %>


To learn more about Kentico's search result transformations, check out the search result transformation documentation


 
Share: