postgresqlfull-text-searchpattern-matchingtrigram

PostgreSQL full text search abbreviations


I created a Postgresql full text search using 'german'. How can I configer, that when I search for "Bezirk", lines containing "Bez." are also a match? (And vice-versa)


Solution

  • @pozs is right. You need to use a synonym dictionary.

    1 - In the directory $SHAREDIR/tsearch_data create the file german.syn with the following contents:

    Bez Bezirk
    

    2 - Execute the query:

    CREATE TEXT SEARCH DICTIONARY german_syn (
        template = synonym,
        synonyms = german);
    CREATE TEXT SEARCH CONFIGURATION german_syn(COPY='simple');
    ALTER TEXT SEARCH CONFIGURATION german_syn
        ALTER MAPPING FOR asciiword, asciihword, hword_asciipart,
            word, hword, hword_part
        WITH german_syn, german_stem;
    

    Now you can test it. Execute queries:

    test=# SELECT to_tsvector('german_syn', 'Bezirk') @@ to_tsquery('german_syn', 'Bezirk & Bez');
     ?column? 
    ----------
     t
    (1 row)
    
    test=# SELECT to_tsvector('german_syn', 'Bez Bez.') @@ to_tsquery('german_syn', 'Bezirk');
     ?column? 
    ----------
     t
    (1 row)
    

    Additional links:

    1. PostgreSQL: A Full Text Search engine (expired)