HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Boost with filters

Describes how to boost the score of hits based on certain criteria, which might be useful in some scenarios.

By default, search results are sorted according to score (relevance) in Optimizely Search & Navigation.

One way to boost the hit score is the BoostMatching method, which has two parameters:

  • Filter expression – Any expression that returns a filter, meaning you can pass it the same types of expressions that you can pass to the Filter method.
  • Boost factor – If you pass it 2, a hit that matches the filter and would have a score of 0.2 instead has a score of 0.4. So, that hit is returned above a hit with a score of 0.3 that does not match the filter.

The following code increases the probability that a blog post about the fruit banana is sorted before a blog post about the brand Banana Republic.

searchResult = client.Search<BlogPost>()
  .For("Banana")
  .BoostMatching(x => x.BlogCategory.Match("fruit"), 2)                
  .GetResult();

You can only call the BoostMatching method when searching (that is, you are not finding all documents of a certain type or using the Filter method). You must call the method before any method not related to the search query (such as Filter, Take, and Skip). This is enforced by the fact that the For method in the above sample returns a IQueriedSearch object, while the Filter method does not. So, if the code compiles, it should work.

You can call the method multiple times. If a hit matches several filters, the boost is accumulated. While calling the method five or ten times is fine, you should not apply a huge number of boosts. For example, you may use the BoostMatching method to boost recently published blog posts by giving ones published today a significant boost and ones published in the last 30 days a slight boost. But adding a different boost for the last 365 days results in a very slow query or even an exception.

Suppose you are developing a site for a car dealer and index instances of the following Car class.

public class Car
  {
    public string Make { get; set; }
    public string Model { get; set; }
    public string Description { get; set; }
    public double SalesMargin { get; set; }
    public bool InStock { get; set; }
    public bool NewModelComingSoon { get; set; }
  }

When a visitor performs a search, you want to return results that match the search query and order them according to relevance. But you may want to tweak the sorting a bit to optimize for the car dealer's business conditions. For example, if a certain model is in stock, the dealership can deliver it and receive payment faster, so you want to boost hits where that is true.

var searchResult = client.Search<Car>()
  .For("Volvo")
  .BoostMatching(x => x.InStock.Match(true), 1.5)
  .GetResult();

Also, if the dealer has a high-profit margin for a certain model, you could boost those cars.

var searchResult = client.Search<Car>()
  .For("Volvo")
  .BoostMatching(x => x.InStock.Match(true), 1.5)
  .BoostMatching(x => x.SalesMargin.GreaterThan(0.2), 1.5)
  .GetResult();

Finally, if a model will soon be replaced by a newer model and the older model is in stock, it might be valuable to sell it before the new model comes out and the older model's value decreases. So, you give a significant boost to hits that match those criteria.

var searchResult = client.Search<Car>()
  .For("Volvo")
  .BoostMatching(x => x.InStock.Match(true), 1.5)
  .BoostMatching(x => x.SalesMargin.GreaterThan(0.2), 1.5)
  .BoostMatching(x => x.NewModelComingSoon.Match(true) 
                    & x.InStock.Match(true), 5)
  .GetResult();

In this example, you adapt the scoring (that is, sorting) of search results to optimize them for business. In other situations, you may want to optimize results for the user. For example, on a culinary site, you boost hits for certain recipes depending on what you know about a logged-in user's allergies, previous searches, or previously-printed recipes.

Combine with other methods

The BoostMatching method is not limited to text searches. You can also use it after the MoreLike method. And, considering the large number of filtering options, you can combine BoostMatching with geographical filtering methods to boost hits close to the user.