November Happy Hour will be moved to Thursday December 5th.

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, you want to boost the score of hits based on certain criteria. One way to do that 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.

Examples

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".

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

You can only call the BoostMatching method when doing a search (that is, you are not finding all documents of a certain type or using the Filter method). The method must be called 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. Note however that, while calling the method five or ten times is fine, it is not a good idea to 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.

Imagine you are developing a site for a car dealer and index instances of the following 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, you want to return results that match the search query, and order them according 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.

C#
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.

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 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.

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, 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 users' allergies, previous searches, or previously-printed recipes. 

Combining BoostMatching 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.

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

Last updated: Feb 23, 2015

Recommended reading