2016-03-04 88 views
0

我使用'german'创建了Postgresql全文搜索。我怎样才能确定,当我搜索“Bezirk”时,包含“Bez”的行。也是一场比赛? (反之亦然)PostgreSQL全文搜索缩写

+1

你需要创建一个自定义[同义词词典](http://www.postgresql.org/docs/current/ static/textsearch-dictionaries.html#TEXTSEARCH-SYNONYM-DICTIONARY)如果你想将它与文字搜索相匹配 – pozs

+0

@pozs似乎是你的建议是路要走。当你把它作为答案来制定时,我会接受它。 – JohnDoe

回答

1

@pozs是正确的。您需要使用synonym dictionary

1 - 目录$ SHAREDIR/tsearch_data创建包含以下内容的文件german.syn:

Bez Bezirk 

2 - 执行查询:

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; 

现在可以测试它。执行查询:

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) 

其他链接:

  1. PostgreSQL: A Full Text Search engine
0

尝试在搜索中使用通配符。

例如:

tableName.column LIKE 'Bez%' 

%将为Bez

+0

关键是“Bez”。存储在数据库中,我查询“Bezirk”,所以查询是比你想象和建议的其他方式。 – JohnDoe

+0

对问题的描述非常模糊,因此很难确定您正在尝试执行的操作。对不起,我无法协助你。祝你的问题好运。 –

0

描述后的任何字母或数字搜索是非常模糊的了解你想要达到的,但它看起来像你需要简单pattern matching搜索当你寻找缩写(所以需要做像全文搜索词干)。我会pg_trgm为了这个目的:

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

结果:

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