概述
Prefix support for text search synonym dictionary.
词典模板被用来创建用于同义词替换的词典。不支持短语。一个同义词词典可以被用来解决语言学问题,例如,阻止一个英语词干分析器词典把词“Paris”缩减成“pari”。在同义词词典中有一行Paris paris并把它放在english_stem词典之前就足够了。
实验
ts_debug
一个自定义文本搜索配置的行为很容易变得混乱。
本节中描述的函数对于测试文本搜索对象有用。你可以测试一个完整的配置,或者独立测试解析器和词典。
ts_debug([ config regconfig, ] document text,
OUT alias text,
OUT description text,
OUT token text,
OUT dictionaries regdictionary[],
OUT dictionary regdictionary,
OUT lexemes text[])
returns setof record
ts_debug显示document的每一个记号的信息,记号由解析器产生并由配置的词典处理过。该函数使用由config指定的配置,如果该参数被忽略则使用default_text_search_config指定的配置。
ts_debug为解析器在文本中标识的每一个记号返回一行。被返回的列是:
alias text — 记号类型的短名称
description text — 记号类型的描述
token text — 记号的文本
dictionaries regdictionary[] — 配置为这种记号类型选择的词典
dictionary regdictionary — 识别该记号的词典,如果没有词典能识别则为NULL
lexemes text[] — 识别该记号的词典产生的词位,如果没有词典能识别则为NULL;一个空数组({})表示该记号被识别为一个停用词
复制
postgres=# SELECT * FROM ts_debug('english', 'Paris');
alias | description | token | dictionaries | dictionary | lexemes
-----------+-----------------+-------+----------------+--------------+---------
asciiword | Word, all ASCII | Paris | {english_stem} | english_stem | {pari}
(1 row)
[root@bogon tsearch_data]# cp opt/pgsql12/share/tsearch_data/synonym_sample.syn opt/pgsql12/share/tsearch_data/my_synonyms.syn
[root@bogon tsearch_data]# cat opt/pgsql12/share/tsearch_data/my_synonyms.syn
postgres pgsql
postgresql pgsql
postgre pgsql
gogle googl
indices index*
一个星号(*)可以被放置在配置文件中一个同义词的末尾。这表示该同义词是一个前缀。
当项被用在to_tsvector()中时,星号会被忽略;当它被用在to_tsquery()中时,结果将是一个带有前缀匹配标记器的查询项。
postgres=# CREATE TEXT SEARCH DICTIONARY my_synonym (
postgres(# TEMPLATE = synonym,
postgres(# SYNONYMS = my_synonyms
postgres(# );
CREATE TEXT SEARCH DICTIONARY
postgres(# CREATE TEXT SEARCH DICTIONARY^C
postgres=# ALTER TEXT SEARCH CONFIGURATION english
postgres-# ALTER MAPPING FOR asciiword
postgres-# WITH my_synonym, english_stem;
ALTER TEXT SEARCH CONFIGURATION
postgres=# SELECT * FROM ts_debug('english', 'Paris');
alias | description | token | dictionaries | dictionary | lexemes
-----------+-----------------+-------+---------------------------+--------------+---------
asciiword | Word, all ASCII | Paris | {my_synonym,english_stem} | my_synonym | {paris}
(1 row)
复制
postgres=# CREATE TEXT SEARCH DICTIONARY syn1 (template=synonym, synonyms='synonym_sample');
CREATE TEXT SEARCH DICTIONARY
postgres=# SELECT ts_lexize('syn1','indices');
ts_lexize
-----------
{asdasd}
(1 row)
synonym_sample.syn syn1字典中,indices的内容asdasd
[root@bogon tsearch_data]# cat opt/pgsql12/share/tsearch_data/synonym_sample.syn
postgres pgsql
postgresql pgsql
postgre pgsql
gogle googl
indices asdasd*
postgres=# CREATE TEXT SEARCH CONFIGURATION tst (copy=simple);
CREATE TEXT SEARCH CONFIGURATION
postgres=# ALTER TEXT SEARCH CONFIGURATION tst ALTER MAPPING FOR asciiword WITH syn;
ALTER TEXT SEARCH CONFIGURATION
postgres=# SELECT to_tsvector('tst','indices');
to_tsvector
-------------
'asdasd':1
(1 row)
postgres=# SELECT to_tsquery('tst','indices');
to_tsquery
------------
'asdasd':*
(1 row)
复制
PostgreSQL提供了函数to_tsvector将一个文档转换成tsvector数据类型。
to_tsvector([ config regconfig, ] document text) returns tsvector
复制
PostgreSQL提供了函数to_tsquery、plainto_tsquery、phraseto_tsquery以及
websearch_to_tsquery用来把一个查询转换成tsquery数据类型。
to_tsquery提供了比plainto_tsquery和phraseto_tsquery更多的特性,但是它对其输入要求更加严格。websearch_to_tsquery是to_tsquery的一个简化版本,
它使用一种可选择的语法,类似于Web搜索引擎使用的语法。
to_tsquery([ config regconfig, ] querytext text) returns tsquery
复制
参考
http://postgres.cn/docs/12/textsearch-dictionaries.html
文章转载自CP的PostgreSQL厨房,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
外国CTO也感兴趣的开源数据库项目——openHalo
小满未满、
2066次阅读
2025-04-21 16:58:09
QPlus V6.3 更新,新增PostgreSQL与PolarDB PG支持,OceanBase 容灾管理重磅上线
沃趣科技
264次阅读
2025-05-13 09:39:27
4月“墨力原创作者计划”获奖名单公布!
墨天轮编辑部
208次阅读
2025-05-13 16:21:59
中国PostgreSQL培训认证体系新增PGAI应用工程师方向
开源软件联盟PostgreSQL分会
193次阅读
2025-05-06 10:21:13
华象新闻 | PostgreSQL 18 Beta 1、17.5、16.9、15.13、14.18、13.21 发布
严少安
170次阅读
2025-05-09 11:34:10
PG生态赢得资本市场青睐:Databricks收购Neon,Supabase融资两亿美元,微软财报点名PG
老冯云数
170次阅读
2025-05-07 10:06:22
SQL 优化之 OR 子句改写
xiongcc
152次阅读
2025-04-21 00:08:06
告别老旧mysql_fdw,升级正当时
NickYoung
128次阅读
2025-04-29 11:15:18
PostgreSQL中文社区亮相于第八届数字中国峰会
PostgreSQL中文社区
121次阅读
2025-05-07 10:06:20
PostgreSQL的dblink扩展模块使用方法
szrsu
116次阅读
2025-04-24 17:39:30