This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.
We have received many requests on how to achieve a partial match functionality using the Sitefinity search. By default Lucene uses exact match and if you search for “choco” you will not get any results for that have “chocolate”.
In order to achieve this functionality, we need to modify the search query to include a wildcard - *. That way Lucene will perform a wildcard search and all words that start with “choco” will be returned as results.
To achieve this, we will need to inherit from the SearchBox and SearchResults widgets and manipulate the query to add the wildcard and pass it to the search. After that, we need to remove the * from the query displayed in the search results, this way the user will be unaware that the wildcard has been added to the search term.using System;using System.Collections.Generic;using System.Linq;using System.Web;using Telerik.Sitefinity.Services.Search.Web.UI.Public;using Telerik.Sitefinity.Services;using Telerik.Sitefinity.Web.UI;using System.Collections.Specialized;namespace SitefinityWebApp{ public class SearchBoxCustom : SearchBox { protected override void InitializeControls(Telerik.Sitefinity.Web.UI.GenericContainer container) { base.InitializeControls(container); if (!this.IsEmpty) { // Set the search text box if searchQuery exists in the QueryString and the IndexCatalogue mathces the current one. var context = SystemManager.CurrentHttpContext; if (context != null) { string searchQuery = context.Request.QueryString["searchQuery"]; if (!string.IsNullOrEmpty(searchQuery)) { string indexCatalogue = context.Request.QueryString["indexCatalogue"]; if (!string.IsNullOrEmpty(indexCatalogue)) { if (indexCatalogue.Equals(this.IndexCatalogue)) { if (!searchQuery.EndsWith("*")) { String currurl = HttpContext.Current.Request.RawUrl; NameValueCollection nameValues = HttpUtility.ParseQueryString(currurl); nameValues.Set("searchQuery", searchQuery + "*"); string updatedString = HttpUtility.UrlDecode(nameValues.ToString()); context.Response.Redirect(updatedString); } // replace the "*" with " " in the search query this.SearchTextBox.Text = searchQuery.Replace('*',' '); } } } } } return; } }}using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using Telerik.Sitefinity.Services;using Telerik.Sitefinity.Services.Search.Web.UI.Public;namespace SitefinityWebApp{ public class CustomSearchResult : SearchResults { protected override void InitializeControls(Telerik.Sitefinity.Web.UI.GenericContainer container) { base.InitializeControls(container); var context = SystemManager.CurrentHttpContext; if (context != null) { string searchTitle = context.Request.QueryString["searchQuery"]; if (!string.IsNullOrEmpty(searchTitle)) { string indexCatalogue = context.Request.QueryString["indexCatalogue"]; if (!string.IsNullOrEmpty(indexCatalogue)) { if (indexCatalogue.Equals(this.IndexCatalogue)) { this.ResultsStats.Text = this.ResultsStats.Text.Replace(searchTitle, searchTitle.TrimEnd('*')); } } } } } }}Subscribe to get all the news, info and tutorials you need to build better business apps and sites