The content you're reading is getting on in years
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(
'*'
));
}
}
}
}
}
}
}
Atanas Valchev is a Tech Support Engineer at Telerik. He joined the Sitefinity Support team in March 2012.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites
Progress collects the Personal Information set out in our Privacy Policy and the Supplemental Privacy notice for residents of California and other US States and uses it for the purposes stated in that policy.
You can also ask us not to share your Personal Information to third parties here: Do Not Sell or Share My Info
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.
Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.