How to create simple fuzzy search with Postgresql only?

前端 未结 2 771
逝去的感伤
逝去的感伤 2020-12-07 11:04

I have a little problem with search functionality on my RoR based site. I have many Produts with some CODEs. This code can be any string like \"AB-123-lHdfj\". Now I use ILI

2条回答
  •  执笔经年
    2020-12-07 11:42

    Postgres provides a module with several string comparsion functions such as soundex and metaphone. But you will want to use the levenshtein edit distance function.

    Example:
    
    test=# SELECT levenshtein('GUMBO', 'GAMBOL');
     levenshtein
    -------------
               2
    (1 row)
    

    The 2 is the edit distance between the two words. When you apply this against a number of words and sort by the edit distance result you will have the type of fuzzy matches that you're looking for.

    Try this query sample: (with your own object names and data of course)

    SELECT * 
    FROM some_table
    WHERE levenshtein(code, 'AB123-lHdfj') <= 3
    ORDER BY levenshtein(code, 'AB123-lHdfj')
    LIMIT 10
    

    This query says:

    Give me the top 10 results of all data from some_table where the edit distance between the code value and the input 'AB123-lHdfj' is less than 3. You will get back all rows where the value of code is within 3 characters difference to 'AB123-lHdfj'...

    Note: if you get an error like:

    function levenshtein(character varying, unknown) does not exist
    

    Install the fuzzystrmatch extension using:

    test=# CREATE EXTENSION fuzzystrmatch;
    

提交回复
热议问题