We have an website created in Umbraco CMS and I am developing a site search functionality. There is a section within the website called "news" where regular updates are published. While listing results from news section (among other pages), I want to bring latest content earlier on the results, e.g. if someone is searching for "exam results", I would like to bring matching news pages created in 2018 earlier than a page created in 2017 and so on. If there an way to boost (either query time or index time) so that latest pages are boosted up?
Below is my code I have written till now:
var page = 1;
var pageSize = 5;
if (Request.QueryString["q"] != null)
searchQuery = Request.QueryString["q"];
if (Request.QueryString["page"] != null)
Int32.TryParse(Request.QueryString["page"], out page);
ISearchResults searchResults = null;
BaseSearchProvider searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
var headerFields = new[] { "contentTitle", "metaTags", "metaDescription", "nodeName" };
var contentFields = new[] { "contentDescription", "mainBody" };
var criteria = searcher.CreateSearchCriteria(IndexTypes.Content, BooleanOperation.Or);
var searchTerm = string.IsNullOrEmpty(Request["q"]) ? string.Empty : Request["q"];
if (searchTerm != string.Empty)
{
searchTerm = searchTerm.MakeSearchQuerySafe();
if (searchTerm.Length > 0)
{
searchTerm = searchTerm.Trim();
}
var examineQuery = criteria.GroupedOr(headerFields, searchTerm.Boost(100));
examineQuery.Or().GroupedOr(contentFields, searchTerm.Boost(50));
if (searchTerm.Contains(" "))
{
examineQuery.Or().GroupedOr(headerFields, searchTerm.RemoveStopWords().Split(' ').Select(x => x.MultipleCharacterWildcard().Value.Boost(10)).ToArray());
examineQuery.Or().GroupedOr(contentFields, searchTerm.RemoveStopWords().Split(' ').Select(x => x.MultipleCharacterWildcard()).ToArray());
}
searchResults = searcher.Search(examineQuery.Compile(), maxResults: pageSize * page);
}
Since others might encounter the same issue and land on this question (through search), I am posting an answer to my own question.
I wrote two event handlers as below:
The event handler code is below: