Stripping Special Characters from Simple Search
Posted: Thu Aug 15, 2013 5:36 pm
For a long time we have had a problem with no search results being displayed when the visitor enters a " or / in the search field. We use the simple search in the left column. Here is the code for the modifications I made to strip all special characters from the search keyword.
The original code in the file SearchPage.asc.cs looks like this
_Keywords = StringHelper.StripHtml(Request.QueryString["k"]);
if (Store.GetCachedSettings().FullTextSearch) _FilteredKeywords = new StopWordsParser().Filter(_Keywords);
else _FilteredKeywords = _Keywords;
I modified the code to look like this.
_Keywords = StringHelper.StripHtml(Request.QueryString["k"]);
string cleanSearchPhrase ="";
for (int i = 0; i < _Keywords.Length; i++)
{
if (_Keywords >= '0' && _Keywords <= '9' || _Keywords >= 'A' && _Keywords <= 'Z' || _Keywords >= 'a' && _Keywords <= 'z')
{
cleanSearchPhrase=cleanSearchPhrase+_Keywords;
}
else
{
cleanSearchPhrase=cleanSearchPhrase+" ";
}
}
_Keywords=cleanSearchPhrase;
if (Store.GetCachedSettings().FullTextSearch) _FilteredKeywords = new StopWordsParser().Filter(_Keywords);
else _FilteredKeywords = _Keywords;
That code will take a search phrase that looks like this.
1 1/4" handlebars w/ 1 1/4" dia. clamping area
and change it to look like this.
1 1 4 handlebars w 1 1 4 dia clamping area
Search results match the products that have the keywords handlebars and clamping area. Which are the products the visitor wants to see.
There are two files named
SimpleSearch.aspx and SimpleSearch.aspx.cs
I wasted a lot of time trying to make this work using the button click routines. It appears those two modules do not get control when the search button is clicked.
The original code in the file SearchPage.asc.cs looks like this
_Keywords = StringHelper.StripHtml(Request.QueryString["k"]);
if (Store.GetCachedSettings().FullTextSearch) _FilteredKeywords = new StopWordsParser().Filter(_Keywords);
else _FilteredKeywords = _Keywords;
I modified the code to look like this.
_Keywords = StringHelper.StripHtml(Request.QueryString["k"]);
string cleanSearchPhrase ="";
for (int i = 0; i < _Keywords.Length; i++)
{
if (_Keywords >= '0' && _Keywords <= '9' || _Keywords >= 'A' && _Keywords <= 'Z' || _Keywords >= 'a' && _Keywords <= 'z')
{
cleanSearchPhrase=cleanSearchPhrase+_Keywords;
}
else
{
cleanSearchPhrase=cleanSearchPhrase+" ";
}
}
_Keywords=cleanSearchPhrase;
if (Store.GetCachedSettings().FullTextSearch) _FilteredKeywords = new StopWordsParser().Filter(_Keywords);
else _FilteredKeywords = _Keywords;
That code will take a search phrase that looks like this.
1 1/4" handlebars w/ 1 1/4" dia. clamping area
and change it to look like this.
1 1 4 handlebars w 1 1 4 dia clamping area
Search results match the products that have the keywords handlebars and clamping area. Which are the products the visitor wants to see.
There are two files named
SimpleSearch.aspx and SimpleSearch.aspx.cs
I wasted a lot of time trying to make this work using the button click routines. It appears those two modules do not get control when the search button is clicked.