配置示例

文本搜索配置(Text Search Configuration),指定了将文档转换成tsvector过程中所必需的组件:

  • 解析器,用于把文本分解成标记token。
  • 词典列表,用于将每个token转换成词位lexeme。

每次to_tsvector或to_tsquery函数调用时,都需要指定一个文本搜索配置来指定具体的处理过程。GUC参数default_text_search_config指定了默认的文本搜索配置,当文本搜索函数中没有显式指定文本搜索配置参数时,将会使用该默认值进行处理。

openGauss中预定义有一些可用的文本搜索配置,用户也可创建自定义的文本搜索配置。此外,为了便于管理文本搜索对象,还提供有多个gsql元命令,可以显示有关文本搜索对象的信息(详细请参见《工具参考》中“客户端工具 >元命令参考”章节)。

操作步骤

  1. 创建一个文本搜索配置ts_conf,复制预定义的文本搜索配置english。

    openGauss=# CREATE TEXT SEARCH CONFIGURATION ts_conf ( COPY = pg_catalog.english );
    CREATE TEXT SEARCH CONFIGURATION
    
  2. 创建Synonym词典。

    假设同义词词典定义文件pg_dict.syn内容如下:

    postgres    pg 
    pgsql       pg 
    postgresql  pg
    

    执行如下语句创建Synonym词典:

    openGauss=# CREATE TEXT SEARCH DICTIONARY pg_dict (
         TEMPLATE = synonym,
         SYNONYMS = pg_dict,
         FILEPATH = 'file:///home/dicts'
     );
    
  3. 创建一个Ispell词典english_ispell(词典定义文件来自开源词典)。

    openGauss=# CREATE TEXT SEARCH DICTIONARY english_ispell (
        TEMPLATE = ispell,
        DictFile = english,
        AffFile = english,
        StopWords = english,
        FILEPATH = 'file:///home/dicts' 
    );
    
  4. 设置文本搜索配置ts_conf,修改某些类型的token对应的词典列表。关于token类型的详细信息,请参见解析器

    openGauss=# ALTER TEXT SEARCH CONFIGURATION ts_conf
        ALTER MAPPING FOR asciiword, asciihword, hword_asciipart,
                          word, hword, hword_part
        WITH pg_dict, english_ispell, english_stem;
    
  5. 在文本搜索配置中,选择设置不索引或搜索某些token类型。

    openGauss=# ALTER TEXT SEARCH CONFIGURATION ts_conf
        DROP MAPPING FOR email, url, url_path, sfloat, float;
    
  6. 使用文本检索调测函数ts_debug()对所创建的词典配置ts_conf进行测试。

    openGauss=# SELECT * FROM ts_debug('ts_conf', '
    PostgreSQL, the highly scalable, SQL compliant, open source object-relational
    database management system, is now undergoing beta testing of the next
    version of our software.
    ');
    
  7. 可以设置当前session使用ts_conf作为默认的文本搜索配置。此设置仅在当前session有效。

    openGauss=# \dF+ ts_conf
          Text search configuration "public.ts_conf"
    Parser: "pg_catalog.default"
          Token      |            Dictionaries             
    -----------------+-------------------------------------
     asciihword      | pg_dict,english_ispell,english_stem
     asciiword       | pg_dict,english_ispell,english_stem
     file            | simple
     host            | simple
     hword           | pg_dict,english_ispell,english_stem
     hword_asciipart | pg_dict,english_ispell,english_stem
     hword_numpart   | simple
     hword_part      | pg_dict,english_ispell,english_stem
     int             | simple
     numhword        | simple
     numword         | simple
     uint            | simple
     version         | simple
     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)
    
意见反馈
编组 3备份
openGauss 2024-03-19 00:48:12
取消