简体   繁体   中英

PHP MySQL based search with approximate matching

I would like to make my search feature to work more smartly in case of typo or product name special character.

For example, we have a product with name "Post-it" and we want to show it if users type "Post it" or "Postit".
Another example, we have a product with name "bic clic stic", we want to show it if the user searches for "bic clic stick" since it has a close match.

Our current query is like:

SELECT name, image, sku, description FROM products WHERE name like '%KEYWORD%' AND ....

Most methods for approaching this problem are not particularly efficient. That is, they still require full table scans (although some optimizations are available).

The technical solution is an algorithm called Levenshtein distance (or more generically, edit distance). This is a method for measuring the distance between two strings, and it works quite well for the examples in your question.

You can google "MySQL Levenshtein" to get various implementations.

Do note that the implementations are not efficient; they require full table scans. The resulting query would look like:

SELECT name, image, sku, description
FROM products
WHERE levenshtein(name, 'KEYWORD') <= 3; -- or some threshhold value

Another approach is to pre-proces the search word (according to some custom rules you will set, eg break into autonomous words) and then concatenate those words with % and search using Mysql's LIKE (or even REGEX ) feature.

This requires no extra add-on for mysql nor re-arranging your already existing data tables . Plus the rules can change dynamicaly for your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM