Categories
Bloggers
Blogs RSS feed

Implementing Sitefinity Partial Match Search

by Atanas Valchev

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

1 comment

Leave a comment
  1. Steve Nov 01, 2013
    ...but if they have actually searched for "choco*" then it strips it out for a re-search?

    Leave a comment