Improve my Search engine
I have tried to implement a simple search engine for my application. This is what I have done:
CREATE FULLTEXT INDEX item_name_other_name_desc_index
ON item (name,other_name,description)
public static function Search($string)
{
$delims = ',.; ';
$word = strtok($string,$delims);
while($word)
{
$result['accepted'][] = $word;
$word = strtok($delims);
}
$string = implode(" ", $result['accepted']);
$sql = 'SELECT item_id,name,other_name,description,price,discounted_price, thumbnail_photo,large_photo
FROM item
WHERE
MATCH(name,other_name,description)
AGAINST(:string)' ;
$parameters = array(':string' => $string);
$result['items'] = DB::GetAll($sql,$parameters);
return $result;
}
This works and searches for words only. Can anyone suggest me how can i improve it and how do i proceed if i want to search for strings like "Bi" which will show me results开发者_如何学编程 starting with that string.(for eg Bike..)
You could use the MySQL Fulltext-Search in Boolean mode. This allows searches like 'Bi*'. You only would have to append the * to each accepted word and change the SELECT a bit.
SELECT item_id,name,other_name,description,price,
discounted_price,thumbnail_photo,large_photo
FROM item
WHERE
MATCH(name,other_name,description)
AGAINST(:string IN BOOLEAN MODE)
where :string is filled with 'Bi*'
you can use wildcards like '%' and '_' ... for your example, having a search key like Bi% will include Bike and any other words that starts with 'Bi' in the result set.... and so on...
精彩评论