MYSQL search for right words | fixing spelling errors

后端 未结 3 711
暖寄归人
暖寄归人 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: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

提交回复
热议问题