Skip to content

Instantly share code, notes, and snippets.

@RyanABailey
Last active March 9, 2016 02:20
Show Gist options
  • Select an option

  • Save RyanABailey/836035d019503231ba3d to your computer and use it in GitHub Desktop.

Select an option

Save RyanABailey/836035d019503231ba3d to your computer and use it in GitHub Desktop.
Sitecore boost template type
pubic class Searcher
{
var searchIndex = ContentSearchManager.GetIndex("MySearchIndex"); // Get the search index
var searchPredicate = BuildSearchPredicate(searchRequest); // Build the search predicate
using (var searchContext = searchIndex.CreateSearchContext()) // Get a context of the search index
{
var searchResults = searchContext.GetQueryable<SearchModel>().Where(searchPredicate); // Search the index for items which match the predicate
}
}
public static Expression<Func<SearchModel, bool>> BuildSearchPredicate(SearchRequestModel searchRequest)
{
var predicate = PredicateBuilder.True<SearchModel>(); // Items which meet the predicate
predicate = predicate.And(GetEqualsSearchPredicate(searchRequest.SearchTerm));
// Boost templates
predicate = predicate.And(GetBoosterPredicate());
return predicate;
}
private static Expression<Func<SearchModel, bool>> GetBoosterPredicate()
{
var predicate = PredicateBuilder.True<SearchModel>(); // Items which meet the predicate
predicate = predicate.Or(x => x.TemplateName == "Content Page").Boost(4.0f);
predicate = predicate.Or(x => x.TemplateName != "Content Page");
return predicate;
}
private static Expression<Func<SearchModel, bool>> GetSearchPredicate(string searchTerm)
{
var predicate = PredicateBuilder.True<SearchModel>(); // Items which meet the predicate
predicate = predicate.Or(x => x.DispalyName.Equals(searchTerm)).Boost(2.0f);
predicate = predicate.Or(x => x.Introduction.Equals(searchTerm)).Boost(2.0f);
predicate = predicate.Or(x => x.Maintext.Equals(searchTerm)).Boost(2.0f);
// Cut down logic here for demo purposes...
return predicate;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment