Customize how Best Bets are applied to external content
A Episerver Find search index can contain a combination of Episerver content – such as pages and blocks served via content providers -, external content – via connectors – and custom objects that’s pushed via custom indexing. Out of the box, Best Bets in Episerver Find nicely supports that a search administrator can promote a given Episerver page or a piece of external content via the Best Bet interface to affect the search results and optimize the relevance.
One thing, that we as a team previously needed was the ability to apply Best Bets to custom indexed objects that is served via Episerver partial routing.
Our index contains more than 20,000 objects that, via routing, are served as a separate page. Imagine that I am a big real-estate investor. You browse my site, http://www.caspersrentals.com/rentals/411-bleecker-st-greenwich-newyork/, to look at a nice rental apartment in adorable Greenwich, New York City. Key point is, that the HTTP request is being intercepted by a partial router, that finds the given rental among 20,000 properties based on the URL segment, and enables Episerver to respond with a rental page serving the information associated. Having this approach to external content – being our property information objects- has a draw back: it does not promote a given rental property, in our search results, even though a best bet for “http://www.caspersrentals.com/rentals/411-e-5th-greenwich-newyork/” is created. Luckily, this can easily be resolved via a custom match definition.
Ensure that custom objects are matched correctly based on an external URL
What we need is a definition of how a URL – for instance “http://www.caspersrentals.com/rentals/411-e-5th-greenwich-newyork/” – matches a specific object, that we want to promote, in our index. We want to define, that “411-e-5th-greenwich-newyork” has to match the UrlSegment$$string element of our custom object.
1. Intercept the creation of Best Bets
It is possible to intercept the create and delete pipeline, for Best Bets, and thereby affect how these are managed. Our main objective is to affect which IBestBetSelector our BestBet associates to. By adjusting this, we will be in full control of detailing the matching expression that’s executed at against our search index.
Our interest is mainly in the code within our ItemSavedEventHandler event handler. It is, if suitable based on the URL input, overwriting the best bet selector, which by default is ExternalUrlBestBetSelector, to our custom DynamicPropertyBestBetSelector implementation. Please be aware that if the URL is not considered suitable – e.g. based on the pattern or structure -, our request to create a Best Bet will instead be managed by Episerver’s default ExternalUrlBestBetHandler.
2. Detail how to match a Best Bet
As you may remember, we want to define that “411-e-5th-greenwich-newyork” has to match the UrlSegment$$string element of our custom indexed object. Trying the Best Bet, identified by a URL, to a search index object is therefore done based on the configured URL.
Method named GetTargetSelectionFilter details how the match is managed. By extracting the unique segment – being “411-e-5th-greenwich-newyork” – and simply requesting UrlSegment$$string to be the match condition is all it takes. Please note, that I’ve left out some minor details, which obviously would dependent on implementation and use. Suitable and GetSegmentFromUrl has two distinct purposes. They are responsible of detailing how the URL is expected to look, in order to be managed by our DynamicPropertyBestBetSelector, and how the segment is extracted.
3. Registering the mechanism
Using above logic requires Episerver to register our handler. It’s required to start the interception of the create and delete pipeline.
It’s fairly simple and only one detail is crucial. The InitializationModule has to dependent on EPiServer.Data.DataInitialization to be registered prior to the built-in ExternalUrlBestBetHandler.
Hopefully this small post provided you with an approach to dealing with Best Bets in Episerver Find. Be aware that it has been taken out of a greater context and are mainly intended to act as a thought starter for your own project.