按 Soundex(或类似)“亲密程度”排序
有什么方法可以让 MySQL 根据结果与搜索词的“听起来”接近程度来对结果进行排序吗?
我正在尝试对包含用户输入的城市名称的字段进行排序。存在变体和拼写错误,我想在顶部显示“最接近”的匹配项。
我知道 soundex 可能不是最好的算法,但如果它(或其他方法)可以合理成功 - 可能值得由数据库完成排序。
Is there any way to have MySQL order results by how close they 'sound' to a search term?
I'm trying to order fields that contain user input of city names. Variations and misspellings exist, and I'd like to show the 'closest' matches at the top.
I know soundex may not be the best algorithm for this, but if it (or another method) could be reasonable successful - it may be worth having the sorting done by the database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Soundex 不适用于此类事情,因为不同的单词可以给您相同的 Soundex 结果,因此会任意排序。更好的解决方案是 Levenshein 编辑距离算法,您可以将其作为数据库中的函数实现:链接到 Levensheint impl。作为MySql存储函数!!!
您还可以查看此SO链接。它包含该算法的 Sql 服务器(特定于 T-SQL)实现,但应该可以移植。该算法的机制相当简单,只需要一个二维数组和循环字符串。
Soundex is no good for this sort of thing because different words can give you the same Soundex results and will therefore sort arbitrarily. A better solution for this is the Levenshein Edit Distance algorithm and you may be able to implement it as a function in your database: Link to Levensheint impl. as MySql stored function!!!
You can also check out this SO link. It contains a Sql server (T-SQL-specific) implementation of the algorithm but it should be possible to port. The mechanics of the algorithm are fairly simple needing only a 2D array and looping over string.