This post describes how it's possible to customise how best bets behave in Episerver Find. It uses an example of matching best bets for specific Episerver locales.
By default Episerver only allows editors to define best bets for site(s) and/or language(s) defined for your site. This works well in most cases, however in scenarios where a site is set up to serve different countries using localisation of the same language it may not work as expected.
For example in the following scenario Episerver localisation can be used to define different country sites:
- site.com/en-GB/ - Great Britain
- site.com/en-AU/ - Australia
- site.com/en-US/ - United States
The works fine but from Find's perspective this is all the same site (site.com) and the same Find language (en) so it's not possible to define a best bet for example for Australia only or the UK only as it can only be defined for site.com in English.
This post describes a possible way to define best bets for specific locales in Episerver Find. Best bet matching in Find is handled by the implementation of IPhraseCriterion. As of Find v12.5.2 it's possible to override or intercept the implementation of IPhraseCriterion.
For this post I am going to implement a convention as follows: If the best bet ends with #[locale] then only apply it for the described locale if the user is browsing in that locale. For example if a user defines the following best bet: Snag #en-AU then it will match the term "Snag" only browsing in the en-AU locale. While Sausage #en-GB would match "Sausage" if browsing in the en-GB locale.
To achieve this the default IPhraseCriterion implementation needs to be replaced with a custom LocaleAwarePhraseCriterion that implements IPhraseCriterion:
The new LocaleAwarePhraseCriterion implementation checks the current locale and ensures the locale matches the defined best bet phrase:
This allows Find administrators to hashtag their best bets when setting them up (click for full size):
Using this approach its possible to extend how best bets are matched using this approach and gives developers more control over the Find implementation which allows for bespoke / specific best bet matches if required.
This is a very simple/quick implementation that allows users to implement best bets for individual locales but could be extended to work any you wished. The code was written as a proof of concept and can be tailored to specific needs so please don’t treat it as production ready!