Implementing Sitefinity Partial Match Search

Implementing Sitefinity Partial Match Search

Posted on November 01, 2013 0 Comments

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.

First, we need to extend the SearchBox like this:

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;
        }
    }
}

In it we do two things - first we get the query and add the * and then continue to redirect to the page holding the SearchResults widget that will do the actual search.

The extension of the SearchResults is very simple and all it does is to remove the * from the search stats term so it will appear as "choco" instead of "choco*"

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('*'));
                        }
                    }
                }
            }
        }
    }
}

A short video showcasing the functionality: http://screencast.com/t/u87NSnTm

Here is a link to the modified controls, just add them to your project and register them with Thunder and they ready to go: ttps://www.dropbox.com/s/9sq9hh0p7c85w8c/PartialMatch.rar

Atanas Valchev

Atanas Valchev is a Tech Support Engineer at Telerik. He joined the Sitefinity Support team in March 2012.

Comments

Comments are disabled in preview mode.
Topics

Sitefinity Training and Certification Now Available.

Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.

Learn More
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Loading animation