Boosting search results
If there are multiple hits for a given search string from different property fields, one can influence Lucene to prioritize a particular property field to search for over the other, resulting in a prioritized search result. This is called boosting.
For example, take a scenario where a user is searching with term "Anna" in a book store, with several books with book title having "Anna" as well as several books written by a author named "Anna". Since its the usual case to search for books by the book title, the hits which are coming from books whose title contains Anna should be more prominent and therefore shown first, and then should all other fields be considered.
Note that boosting may affect the site search performance and therefore be used only when necessary.
Following is a sample method that boost display name over other property fields.
/// <summary>
/// Boost the prominance given to display name when searching.
/// </summary>
/// <param name="searchText">The search text.</param>
/// <param name="languageId">The language unique identifier.</param>
/// <param name="token">The token.</param>
/// <returns></returns>
public static List<Product> BoostSearch(string searchText, Guid languageId, SecurityToken token)
{
var request = new QueryRequest(languageId, ProductCatalogSearchDomains.Products, token);
var textTermClause = new MandatoryTagClause();
foreach (var term in StringHelper.ExtractTerms(searchText))
{
var textClause = new OptionalTagClause();
//Make the display name to have a high prominance by boosting it.
textClause.FilterTags.Add(new Tag(TagNames.Name, searchText) {AllowFuzzy = true, Boost = 100f });
textClause.FilterTags.Add(new Tag(FieldNames.Body, searchText) { AllowFuzzy = true, Boost=10f });
textClause.FilterTags.Add(new Tag(FieldNames.Body, searchText + "*") { Boost = 5f });
textClause.FilterTags.Add(new Tag(FieldNames.Body, "*" + searchText) { Boost = 5f });
textTermClause.FilterTags.Add(textClause);
}
request.FilterTags.Add(textTermClause);
var response = Solution.Instance.SearchService.Search(request);
var productList = Product.GetProducts(response.Hits.Select(x => new Guid(x.Id)));
return productList;
}