数据仓库服务 GAUSSDB(DWS)-文本搜索配置示例:操作步骤

时间:2024-12-24 10:32:37

操作步骤

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

    1
    2
    CREATE TEXT SEARCH CONFIGURATION ts_conf ( COPY = pg_catalog.english );
    CREATE TEXT SEARCH CONFIGURATION
    

  2. 创建Synonym词典。

    假设同义词词典定义文件pg_dict.syn内容如下:
    1
    2
    3
    postgres    pg 
    pgsql       pg 
    postgresql  pg
    

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

    认证用的AK和SK硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全。

    1
    2
    3
    4
    5
    CREATE TEXT SEARCH DICTIONARY pg_dict (
         TEMPLATE = synonym,
         SYNONYMS = pg_dict,
         FILEPATH =  'obs://bucket01/obs.example.com accesskey=xxxxx secretkey=xxxxx region=cn-north-1'
     );
    

  3. 创建一个Ispell词典english_ispell(词典定义文件来自开源词典)。

    1
    2
    3
    4
    5
    6
    7
    CREATE TEXT SEARCH DICTIONARY english_ispell (
        TEMPLATE = ispell,
        DictFile = english,
        AffFile = english,
        StopWords = english,
        FILEPATH =   'obs://bucket01/obs.example.com accesskey=xxxxx secretkey=xxxxx region=cn-north-1'
    );
    

  4. 设置文本搜索配置ts_conf,修改某些类型的token对应的词典列表。关于token类型的详细信息,请参见文本搜索解析器

    1
    2
    3
    4
    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类型。

    1
    2
    ALTER TEXT SEARCH CONFIGURATION ts_conf
        DROP MAPPING FOR email, url, url_path, sfloat, float;
    

  6. 使用文本检索调测函数ts_debug()对所创建的词典配置ts_conf进行测试。

    1
    2
    3
    4
    5
    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有效。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    \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
    
    SET default_text_search_config = 'public.ts_conf';
    SET
    SHOW default_text_search_config;
     default_text_search_config 
    ----------------------------
     public.ts_conf
    (1 row)
    

support.huaweicloud.com/sqlreference-dws/dws_06_0110.html