Boost with filters
Describes how to boost the score of hits based on certain criteria, which might be useful in some scenarios.
Optimizely Search & Navigation by default sorts search results by score (relevance).
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 would score 0.2 instead of 0.4. So, it returns that hit above a hit with a score of 0.3 that does not match the filter.
The following code increases the probability that it sorts a blog post about the fruit banana 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 documents of a certain type or using the Filter
method). You must call the method before any method unrelated to the search query (such as Filter
, Take
, and Skip
). The above sample enforces this by the For
method returning 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 accumulates. While you can call the method five or ten times, you should not apply many 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. However, 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 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 newer model replaces an older one in stock, you may want to sell it before the new one 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 search results scoring (sorting) 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 a large number of filtering options, you can combine BoostMatching
with geographical filtering methods to boost hits close to the user.
Updated 7 months ago