Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MySQL Search Suggestions

In my web application there will be several users. and they have their own contents uploaded to my webapp. For each content they upload it has a title, description and tags(keywords). I can write a search script to search for content or user name. but they keywords when they have given with a spelling mistake it doesn't return any result. For example if there is a user named "Michael" in the database and the search query was "Micheal" i should get "Did you mean to search for 'Michael'" which is none other than a search suggestion.

Also this suggestion should be for the contents uploaded by the user. An user may keep their content's title as "Michael's activities May 2011" and suggestions should be generated for individual words.

like image 901
Jeyanth Kumar Avatar asked Aug 27 '11 11:08

Jeyanth Kumar


2 Answers

You could use SOUNDEX to search for similar-sounding names, like that:

SELECT * FROM users WHERE SOUNDEX(name) = SOUNDEX(:input)

or like that

SELECT * FROM users WHERE name SOUNDS_LIKE :input

(which is completely equivalent)

Edit: if you need to use an algorithm other than Soundex, as Martin Hohenberg suggested, you would need to add an extra column to your table, called, for example, sound_equivalent. (This is actually a more efficient solution as this column can be indexed). The request would then be:

SELECT * FROM users WHERE sound_equivalent = :input_sound_equivalent

The content of the sound_equivalent column can then be generated with a PHP algorithm, and inserted in the table with the rest of user parameters.

like image 190
Philippe Plantier Avatar answered Oct 16 '22 11:10

Philippe Plantier


You can also use the php library pspell to get suggestions if you have no search results.

like image 31
Wesley van Opdorp Avatar answered Oct 16 '22 11:10

Wesley van Opdorp