A client complained that the search result was not changed when they set the weights in the Episerver Find admin view.
If I get it right, those weights are only used when you do a UnifiedSearch (or UnifiedSearchFor). We are doing a .Search and GetContentResult, because we need the result as content.
Is there a way to keep the current funtionality and let the client set weight in admin mode?
I'm guessing that what I want to do is either
Or should I tell the client to ignore the weight setting in admin mode, and we set the boosting in code using .BostMatching?
Thanks for any advice!
Hi. Seems like setting the weights via code is the only option if you are not using UnifiedSearch. That seems weird that the admin ui weights only work with UnifiedSearch. It would be very un-intuative, for instance, if you had different searchs that used both UnifiedSearch and .Search, and weights in the admin worked for one and not the other. Seems like it should work for both.
You should be able to fetch the weights set in the UI
var unifiedWeightsQuery =
SearchClient.Instance.NewCommand(context => new UnifiedWeightsCommand(context, SearchClient.Instance.DefaultIndex));
UnifiedWeightsItem weights = unifiedWeightsQuery.Execute().Hits.FirstOrDefault();
and apply them when specifying .InField(fieldName, relativeImportance)
Usually, there is a sorting option that the user can use (Relevance, Date Asc/Desc). If one is set, the search result won't change because an orderBy has been applied and the result is no longer sorted by score. What we had to do was to end the query with the following extension
.ThenByScore() (Credit Henrik Lindström) https://gist.github.com/lindstromhenrik/9779858
To make sure that the score is actually taken into account after the inital sorting has been applied.
Then we created a custom "Boosting Settings" page type, where the editor can set the boosting multiplier for custom properties, and included that in the search query as well as ITypedSeach extension, something like
Then the function
public static ITypeSearch<T> AddCustomBoosting<T>(this ITypeSearch<T> query, SearchRequest searchRequest)
where T : CustomPageType
var queries = searchRequest.Query.Split(' ');
foreach (var word in queries)
var cleanedWord = QueryEscaping.Quote(word);
query = query
.BoostMatching(x => x.DisplayName.AnyWordBeginsWith(cleanedWord), _siteSettingsPage.Content.BoostDisplayName)
.BoostMatching(x => x.PageTitle.AnyWordBeginsWith(cleanedWord), _siteSettingsPage.Content.BoostPageTitle)
Does this make sense?
Thanks all of you! Hopefully I can make something out of this :-)