Try our conversational search powered by Generative AI!

Loading...
Area: Optimizely Search & Navigation
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

By default search results are sorted according to score (relevance). In some scenarios we want to boost the score of hits based on certain criteria. One way to do that is by using the BoostMatching method. The method has two parameters, a filter expression and a double. The filter expression can be any expression that returns a filter, meaning that we can pass it the same types of expressions that we can pass to the Filter method. The second parameter is the boost factor. If we pass it 2 a hit that matches the filter and otherwise would have had a score of 0.2 will instead have a score of 0.4 and thereby be returned before a hit that has a score of 0.3 but does not match the filter.

Examples

Using the below code we increase the probability that a blog post about the fruit banana is sorted before a blog post about the brand "Banana Republic".

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

The BoostMatching method can only be called when doing a search (as in, we are not just finding all documents of a certain type or just using the Filter method) and has to be called before any method that is not related to the search query (such as Filter, Take, Skip etc). This is enforced by the fact that the For method in the above case returns a IQueriedSearch object while for instance the Filter method does not, so if it compiles it should work.

The method can be called multiple times. If a hit matches several filters the boost will be accumulated. Note however that while calling the method five or ten times is fine, applying a huge number of boosts is not a good idea. For instance, we may use it to boost recently published blog posts by giving those that are published today a significant boost and those published the last 30 days a slight boost. But adding a different boost for the last 365 days will at best result in a very slow query and at worst result in an exception.

Imagine we are developing a site for a car dealer and have indexed instances of the below Car class.

C#
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 we will of course want to limit the results to those that match the search query and order them according to how relevant they are for the query. But we may also want to tweak the sorting a little to optimize for the car dealers business conditions.

If a certain model is in stock they are able to deliver it faster to the customer and lower and receive payment faster so we might boost hits where that is true a little.

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

Also, if the dealer has a very high margin for a certain model we could boost that a little as well.

C#
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 is about to be replaced by a newer model and the dealer has one of in stock it might be very valuable to sell it before the new model comes out and its value is decreased. So we give hits that match that criteria a significant boost.

C#
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 we are adapting the scoring, or the sorting, of search results according to optimize for the business. In other situations we may instead want to optimize for the user. On a site with recipes we may for instance want to boost hits for recipes depending on what we know about a logged in users allergies, previous searches or previously printed recipes. 

Possibilities

The BoostMatching method is not limited to text searches. Meaning that we can also use it after the MoreLike method. And considering the vast number of filtering options supported, we can also use it for instance to combine it with the geographical filtering methods to boost hits close to the user.

Do you find this information helpful? Please log in to provide feedback.

Last updated: Apr 03, 2014

Recommended reading