I had a client point something out I never really thought about.
A product called “SALA Ultra-Lok” is in the database. If you search “sala ultra-lok”, or “sala”, or “ultra-lok”, etc, no problems it is found.
But if you search “sala ultra lok” (or any variation without the hyphen), it will not be found.
I’ve fixed it with this:
$strings = explode(" ", $search);
foreach($strings as $key => $value){
$q .= "
AND (
p.productTitle LIKE '%".$this->escape_string($value)."%' OR
p.productSKU LIKE '%".$this->escape_string($value)."%' OR
b.brandName LIKE '%".$this->escape_string($value)."%'
)";
}
That’s just a part of the module. It just calls the mysql_real_escape_string on the string. The question is more about the search string and breaking it up to make the search more broad.
Because your search string doesnt look for those terms independantly.
% is the wildcard character. Putting in “sala ultra lok” returns the query string: p.productTitle LIKE ‘%sala ultra lok%’ OR …
Which will only return products using that phrase. If you want to tokenize the result, you’re gonna have to reconstruct the code a bit; change the preg_replace to a preg_split, and construct a multiple-or query using each part.
Give example, SL. Okay, here goes.
DB Entries:
sala ultra lok toolkit
sala ultra-lok toolkit
ultra-lok sala toolkit
ultra mega wrench
Search Term: sala ultra lok
Your original query would match 1.
The preg_replace would match 1 and 2.
Preg-splitting and compound-or would match all 4.
Preg-splitting and compound-AND would match 1,2, and 3.
As an alternative, because you are searching 3 fields in a single table, you could look at creating a [google]mysql FULLTEXT index[/google] on those fields, this may well do a better job of finding these “close” matches.
I’d copy that table, set up the index and see if it returns matches that are in line with your clients expectations.