The challenge
Kentico 12 MVC is a flexible framework for website development. It’s incredibly easy for content marketers to use the Page Builder and create great looking pages on their own. However, by default in the current version, the content stored within widgets built using Page Builder cannot be added to a Smart Search index.
To work around this, we could use a Pages Crawler type index, but that could result in false positives and less relevant results due to the header/footer being indexed for each page. Kentico is aware of this limitation and they currently have this slated as part of Milestone 2 on their Kentico 2020 roadmap.
Since Kentico 2020 isn’t scheduled to be released for another six months or so, we developed a custom solution that would meet our client’s needs in the interim.
The solution
After analyzing a few ways we could handle this, we landed on customizing the content of the search index as the most bang for our buck.
We can reference the Kentico documentation for a reference on how to customize the content of a search index.
First, we need to implement an event handler for DocumentsEvents.GetContent.Execute, which occurs right before a page’s content is written to the Smart Search index. To do this, we create a custom module class as described here. This module needs to be accessible by both the Admin and the Website, so it is created in a library that is shared across both solutions.
The second parameter in the event handler is of type DocumentSearchEventArgs which gives us access to the TreeNode. This also gives us the ability to inject custom content prior to it being written to the index. We need to retrieve the content from the DocumentPageBuilderWidgets column from the TreeNode and parse the JSON. To handle this parsing, we can use Json.NET which will allow us to easily ignore the extra content in the JSON object that we don’t care about.
In case we need to easily add more content to this page for indexing, we can create an extension method on DocumentSearchEventArgs to pass in the content to be appended.