I got some help in a previous question about prioritizing the title field in searches, however when I run the search now, I continue to to get some results where the word is only in the content field and not in the title field. These hits are interspersed so that there are some hits that don't have the word in the title coming before some that do. It doesn't seem to matter how much I boost the value for the title field and I've tried to manually decrease the priority of the content field, but that doesn't seem to work either. It seems that the basequery is somehow retrieving all hits for any field, and then after that the boosting only helps so much. Any other tips? Here is the code I'm using:
public class CustomSearchResultItem : SearchResultItem
{
[DataMember]
[IndexField("title")]
public string Title
{
get; set;
}
[DataMember]
[IndexField("_content")]
public string BodyContent { get; set; }
}
public List<CustomSearchResultItem> GetSiteSearch(string searchQuery)
{
string indexName = App.Core.Config.SettingsManager.GetSetting("SearchIndexName");
var index = Sitecore.ContentSearch.ContentSearchManager.GetIndex(indexName);
var db = Sitecore.Context.Database;
Sitecore.Diagnostics.Error.AssertNotNull(index,
"There is no " + indexName + " index on the current database (" + db.Name + ")");
using (var context = index.CreateSearchContext())
{
var baseQuery = PredicateBuilder.True<CustomSearchResultItem>();
var query = PredicateBuilder.True<CustomSearchResultItem>();
if (!string.IsNullOrWhiteSpace(searchQuery))
{
foreach (var word in searchQuery.Split(' '))
{
query = query.Or(item => item.Title.Contains(word)).Boost(1000);
query = query.Or(item => item.BodyContent.Contains(word)).Boost(0);
}
}
baseQuery = baseQuery.And(query);
var queryRunner = context.GetQueryable<CustomSearchResultItem>().Where(baseQuery);
var results = queryRunner.ToList(); /
return results;
}
}
Boost
level to something absurd to see if that affects the results strongly enough?