How do I query for records ordered by similarity?
Eg. searching for “Stock Overflow” would return
- Stack Overflow
- SharePoint Overflow
- Math Overflow
- Politic Overflow
- VFX Overflow
Eg. searching for “LO” would return:
- pabLO picasso
- michelangeLO
- jackson polLOck
What I need help with:
-
Using a search engine to index & search a MySQL table, for better results
-
Using full-text indexing, to find similar/containing strings
What does not work well
- Levenshtein distance is very erratic. (UDF, Query)
Searching for “dog” gives me:- dog
- bog
- ago
- big
- echo
LIKEreturns better results, but returns nothing for long queries although similar strings do exist- dog
- dogid
- dogaral
- dogma
I have found out that the Levenshtein distance may be good when you are searching a full string against another full string, but when you are looking for keywords within a string, this method does not return (sometimes) the wanted results. Moreover, the SOUNDEX function is not suitable for languages other than english, so it is quite limited. You could get away with LIKE, but it’s really for basic searches. You may want to look into other search methods for what you want to achieve. For example:
You may use Lucene as search base for your projects. It’s implemented in most major programming languages and it’d quite fast and versatile. This method is probably the best, as it not only search for substrings, but also letter transposition, prefixes and suffixes (all combined). However, you need to keep a separate index (using CRON to update it from a independent script once in a while works though).
Or, if you want a MySQL solution, the fulltext functionality is pretty good, and certainly faster than a stored procedure. If your tables are not MyISAM, you can create a temporary table, then perform your fulltext search :
Use a data generator to generate some random data if you don’t want to bother creating it yourself…
** NOTE ** : the column type should be
latin1_binto perform a case sensitive search instead of case insensitive withlatin1. For unicode strings, I would recommendutf8_binfor case sensitive andutf8_general_cifor case insensitive searches.Read more about it from the MySQL API reference page
The downside to this is that it will not look for letter transposition or “similar, sounds like” words.
** UPDATE **
Using Lucene for your search, you will simply need to create a cron job (all web hosts have this “feature”) where this job will simply execute a PHP script (i.g. “cd /path/to/script; php searchindexer.php”) that will update the indexes. The reason being that indexing thousands of “documents” (rows, data, etc.) may take several seconds, even minutes, but this is to ensure that all searches are performed as fast as possible. Therefore, you may want to create a delay job to be run by the server. It may be overnight, or in the next hour, this is up to you. The PHP script should look something like this:
Then, this is basically how you search (basic search) :
Here are great sites about Lucene in Java, PHP, and .Net.
In conclusion each search methods have their own pros and cons :
Please feel free to comment if I have forgotten/missed anything.