
# 配置示例
文本搜索配置（Text Search Configuration），指定了将文档转换成tsvector过程中所必需的组件：
- 解析器，用于把文本分解成标记token；
- 词典列表，用于将每个token转换成词位lexeme。
每次to_tsvector或to_tsquery函数调用时，都需要指定一个文本搜索配置来指定具体的处理过程。GUC参数default_text_search_config指定了默认的文本搜索配置，当文本搜索函数中没有显式指定文本搜索配置参数时，将会使用该默认值进行处理。
DWS中预定义有一些可用的文本搜索配置，用户也可创建自定义的文本搜索配置。此外，为了便于管理文本搜索对象，还提供有多个gsql元命令，可以显示有关文本搜索对象的信息（详细请参见《工具指南》中"元命令参考"章节）。
#### 操作步骤
1. 创建一个文本搜索配置ts_conf，复制预定义的文本搜索配置english。 
   ```
   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词典：
   ```
   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（词典定义文件来自开源词典）。 
   ```
   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类型的详细信息，请参见[解析器](https://support.huaweicloud.com/sqlreference-911-dws/dws_06_0101.html)。
   
   ```
   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类型。 
   ```
   ALTER TEXT SEARCH CONFIGURATION ts_conf
       DROP MAPPING FOR email, url, url_path, sfloat, float;
   ```
   
   
6. 使用文本检索调测函数ts_debug()对所创建的词典配置ts_conf进行测试。 
   ```
   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有效。 
   ```
   \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)
   ```
   
   
 
