MYSQL search for right words | fixing spelling errors

后端 未结 3 701
暖寄归人
暖寄归人 2020-12-06 08:54

I have a table dictionary which contains a list of words Like:

   ID|word
   ---------
    1|hello
    2|google
    3|similar
    ...

相关标签:
3条回答
  • 2020-12-06 09:01

    There is a function that does roughly want you want, but it's intensive and will slow queries down. You might be able to use in your circumstances, I have used it before. It's called Levenshtein. You can get it here How to add levenshtein function in mysql?

    0 讨论(0)
  • 2020-12-06 09:08

    What you want to do is called a fuzzy search. You could use the SOUNDEX function in MySQL, documented here:

    http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_soundex

    You query would look like:

    SELECT * FROM dictionary where SOUNDEX(word) = SOUNDEX(:yourSearchTerm)

    ... where your search term is bound to the :yourSearchTerm parameter value.

    A next step would be to try implementing and making use of a Levenshtein function in MySQL. One is described here:

    http://www.artfulsoftware.com/infotree/qrytip.php?id=552

    The Levenshtein distance between two strings is the minimum number of operations needed to transform one string into the other, where an operation may be insertion, deletion or substitution of one character.

    You might also consider looking into databases that are aimed at full text searching, such as Elastic Search, which provides this natively:

    https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html

    0 讨论(0)
  • 2020-12-06 09:12

    you can use soundex() function for comparing phonetically

    your query should be something like:

    select * from table where soundex(word) like soundex('helo');
    

    and this will return you the hello row

    0 讨论(0)
提交回复
热议问题