Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - searching database with the LIKE operator

Given your data stored somewhere in a database:

Hello my name is Tom I like dinosaurs to talk about SQL.  
SQL is amazing. I really like SQL.

We want to implement a site search, allowing visitors to enter terms and return relating records. A user might search for:

Dinosaurs

And the SQL:

WHERE articleBody LIKE '%Dinosaurs%'

Copes fine with returning the correct set of records.

How would we cope however, if a user mispells dinosaurs? IE:

Dinosores

(Poor sore dino). How can we search allowing for error in spelling? We can associate common misspellings we see in search with the correct spelling, and then search on the original terms + corrected term, but this is time consuming to maintain.

Any way programatically?

Edit

Appears SOUNDEX could help, but can anyone give me an example using soundex where entering the search term:

Dinosores wrocks

returns records instead of doing:

WHERE articleBody LIKE '%Dinosaurs%' OR articleBody LIKE '%Wrocks%'

which would return squadoosh?

like image 569
Tom Gullen Avatar asked Mar 03 '11 15:03

Tom Gullen


2 Answers

If you're using SQL Server, have a look at SOUNDEX.

For your example:

select SOUNDEX('Dinosaurs'), SOUNDEX('Dinosores')

Returns identical values (D526) .

You can also use DIFFERENCE function (on same link as soundex) that will compare levels of similarity (4 being the most similar, 0 being the least).

SELECT DIFFERENCE('Dinosaurs', 'Dinosores'); --returns 4

Edit:

After hunting around a bit for a multi-text option, it seems that this isn't all that easy. I would refer you to the link on the Fuzzt Logic answer provided by @Neil Knight (+1 to that, for me!).

This stackoverflow article also details possible sources for implentations for Fuzzy Logic in TSQL. Once respondant also outlined Full text Indexing as a potential that you might want to investigate.

like image 86
James Wiseman Avatar answered Oct 09 '22 16:10

James Wiseman


Perhaps your RDBMS has a SOUNDEX function? You didn't mention which one was involved here.

  • SQL Server's SOUNDEX
like image 39
p.campbell Avatar answered Oct 09 '22 16:10

p.campbell