PostgreSQL full text search abbreviations

筅森魡賤 提交于 2020-04-16 11:48:12

问题


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)


回答1:


@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)



回答2:


Try using a wildcard in your search.

For example:

tableName.column LIKE 'Bez%'

The % will search for any letter or number after the Bez




回答3:


Description is very vague to understand what you are trying to achieve, but it looks like you need simple pattern matching search as you looking for abbreviations (so need to do stemming like in Full Text Search). I would with pg_trgm for this purpose:

WITH t(word) AS ( VALUES
  ('Bez'),
  ('Bezi'),
  ('Bezir')
)
SELECT word, similarity(word, 'Bezirk') AS similarity
FROM t
WHERE word % 'Bezirk'
ORDER BY similarity DESC;

Result:

 word  | similarity 
-------+------------
 Bezir |      0.625
 Bezi  |        0.5
 Bez   |      0.375
(3 rows)


来源:https://stackoverflow.com/questions/35805686/postgresql-full-text-search-abbreviations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!