Customize search results by overriding the SearchResults class

You can customize Sitefinity's search results by using a customized SearchResults widget that inherits the SearchResults class. Then you can register the custom widget in the toolbox and use it on pages and page templates. 

For more information, see Register a new widget in Sitefinity CMS toolbox.

Following is a code sample that demonstrates a customized overridden SearchResults class. It filters the list of search results to display only results published in the last 10 days, by overriding the Search method:

using System;
using System.Collections.Generic;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Services.Search;
using Telerik.Sitefinity.Services.Search.Data;
using Telerik.Sitefinity.Services.Search.Web.UI.Public;
namespace SitefinityWebApp
{
public class CustomSearchResults : SearchResults
{
protected override ISearchResultsBuilder GetSearcher()
{
return new MySearcher(this);
}
protected class MySearcher : ISearchResultsBuilder
{
public MySearcher(SearchResults control)
{
this.control = control;
}
public IEnumerable<IDocument> Search(string query, string catalogue, string[] searchFields, string[] highlightedFields, int skip, int take, out int hitCount)
{
return this.Search(query, catalogue, searchFields, highlightedFields, skip, take, out hitCount, false);
}
public IEnumerable<IDocument> Search(string query, string catalogue, string[] searchFields, string[] highlightedFields, int skip, int take, out int hitCount, bool setLinksOnlyFromCurrentSite)
{
var control = this.control;
var service = Telerik.Sitefinity.Services.ServiceBus.ResolveService<ISearchService>();
var queryBuilder = ObjectFactory.Resolve<IQueryBuilder>();
var searchQuery = queryBuilder.BuildQuery(query, control.SearchFields);
searchQuery.IndexName = catalogue;
searchQuery.Skip = skip;
searchQuery.Take = take;
searchQuery.OrderBy = null;
searchQuery.HighlightedFields = control.HighlightedFields;
// Contains the default filter - by current language
var currentFilter = searchQuery.Filter;
var myDatesFilter = new SearchFilter();
myDatesFilter.Operator = QueryOperator.And;
// Here we add a clause, that only results for the last 10 days will be displayed
myDatesFilter.AddClause(new SearchFilterClause("PublicationDate", DateTime.UtcNow.AddDays(-10), FilterOperator.Greater));
// Persist the language filter, if exists
if (currentFilter != null) myDatesFilter.AddFilter(currentFilter);
searchQuery.Filter = myDatesFilter;
IResultSet result = service.Search(searchQuery);
hitCount = result.HitCount;
return result.SetContentLinks();
}
protected readonly SearchResults control;
}
}
}

Additional resources

Want to learn more?

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?