Created
July 14, 2015 10:22
Search Function With Boost
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void Search() | |
{ | |
string SearchKey = WebUtil.GetQueryString("keyword"); | |
if (string.IsNullOrEmpty(SearchKey)) | |
return; | |
List<Item> results = new List<Item>(); | |
ISearchIndex Index = ContentSearchManager.GetIndex("sitecore_master_index"); | |
using (new Sitecore.Globalization.LanguageSwitcher("en")) | |
{ | |
using (var context = Index.CreateSearchContext()) | |
{ | |
List<Item> ResultList = new List<Item>(); | |
var contentPredicate = PredicateBuilder.True<customSearchResultItem>(); | |
contentPredicate = contentPredicate.And(p => p.Content.Contains(SearchKey)); | |
Sitecore.Data.ID _templateId1 = new Sitecore.Data.ID("{BC073827-8D24-4CCB-9ACB-984028F7341A}"); | |
Sitecore.Data.ID _templateId2 = new Sitecore.Data.ID("{8813914B-AE26-4462-9003-281370082E61}"); | |
var templatePredicate = PredicateBuilder.True<customSearchResultItem>(); | |
templatePredicate = templatePredicate.Or(p => p.TemplateId == _templateId1); | |
templatePredicate = templatePredicate.Or(p => p.TemplateId == _templateId2); | |
string language = Sitecore.Context.Language.ToString(); | |
var languagePredicate = PredicateBuilder.True<customSearchResultItem>(); | |
languagePredicate = languagePredicate.And(p => p.Language == language); | |
var CombinPredicates = PredicateBuilder.True<customSearchResultItem>(); | |
CombinPredicates = CombinPredicates.And(contentPredicate).Boost(1); | |
CombinPredicates = CombinPredicates.And(languagePredicate); | |
CombinPredicates = CombinPredicates.Or(templatePredicate.Boost(1)); | |
// execute the search | |
IQueryable<SearchResultItem> query = context.GetQueryable<customSearchResultItem>().Where(CombinPredicates); | |
results = query.Select(p => p.GetItem()).ToList(); | |
var hits = query.GetResults().Hits; | |
foreach (var item in hits) | |
{ | |
scores.Add(item.Score); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment