配置示例
文本搜索配置(Text Search Configuration),指定了将文档转换成tsvector过程中所必需的组件:
- 解析器,用于把文本分解成标记token。
- 词典列表,用于将每个token转换成词位lexeme。
每次to_tsvector或to_tsquery函数调用时,都需要指定一个文本搜索配置来指定具体的处理过程。GUC参数default_text_search_config指定了默认的文本搜索配置,当文本搜索函数中没有显式指定文本搜索配置参数时,将会使用该默认值进行处理。
openGauss中预定义有一些可用的文本搜索配置,用户也可创建自定义的文本搜索配置。此外,为了便于管理文本搜索对象,还提供有多个gsql元命令,可以显示有关文本搜索对象的信息(详细请参见《工具与命令参考》中“客户端工具 >元命令参考”章节)。
操作步骤
- 创建一个文本搜索配置ts_conf,复制预定义的文本搜索配置english。
1. openGauss=# CREATE TEXT SEARCH CONFIGURATION ts_conf ( COPY = pg_catalog.english );
2. CREATE TEXT SEARCH CONFIGURATION
2. 创建Synonym词典。
假设同义词词典定义文件pg_dict.syn内容如下:
1. postgres pg
2. pgsql pg
3. postgresql pg
执行如下语句创建Synonym词典:
1. openGauss=# CREATE TEXT SEARCH DICTIONARY pg_dict (
2. TEMPLATE = synonym,
3. SYNONYMS = pg_dict,
4. FILEPATH = 'file:///home/dicts'
5. );
3. 创建一个Ispell词典english_ispell(词典定义文件来自开源词典)。
1. openGauss=# CREATE TEXT SEARCH DICTIONARY english_ispell (
2. TEMPLATE = ispell,
3. DictFile = english,
4. AffFile = english,
5. StopWords = english,
6. FILEPATH = 'file:///home/dicts'
7. );
4. 设置文本搜索配置ts_conf,修改某些类型的token对应的词典列表。关于token类型的详细信息,请参见解析器。
1. openGauss=# ALTER TEXT SEARCH CONFIGURATION ts_conf
2. ALTER MAPPING FOR asciiword, asciihword, hword_asciipart,
3. word, hword, hword_part
4. WITH pg_dict, english_ispell, english_stem;
5. 在文本搜索配置中,选择设置不索引或搜索某些token类型。
1. openGauss=# ALTER TEXT SEARCH CONFIGURATION ts_conf
2. DROP MAPPING FOR email, url, url_path, sfloat, float;
6. 使用文本检索调测函数ts_debug()对所创建的词典配置ts_conf进行测试。
1. openGauss=# SELECT * FROM ts_debug('ts_conf', '
2. PostgreSQL, the highly scalable, SQL compliant, open source object-relational
3. database management system, is now undergoing beta testing of the next
4. version of our software.
5. ');
7. 可以设置当前session使用ts_conf作为默认的文本搜索配置。此设置仅在当前session有效。
``` 1. openGauss=# \dF+ ts_conf 2. Text search configuration "public.ts_conf" 3. Parser: "pg_catalog.default" 4. Token | Dictionaries 5. -----------------+------------------------------------- 6. asciihword | pg_dict,english_ispell,english_stem 7. asciiword | pg_dict,english_ispell,english_stem 8. file | simple 9. host | simple 10. hword | pg_dict,english_ispell,english_stem 11. hword_asciipart | pg_dict,english_ispell,english_stem 12. hword_numpart | simple 13. hword_part | pg_dict,english_ispell,english_stem 14. int | simple 15. numhword | simple 16. numword | simple 17. uint | simple 18. version | simple 19. word | pg_dict,english_ispell,english_stem
- openGauss=# SET default_text_search_config = 'public.ts_conf';
- SET
- openGauss=# SHOW default_text_search_config;
- default_text_search_config
-
- public.ts_conf
- (1 row) ```
