暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PostgreSQL特性矩阵解析系列19之 text search synonym dictionary

438

概述

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_tsqueryplainto_tsqueryphraseto_tsquery以及
            websearch_to_tsquery用来把一个查询转换成tsquery数据类型。
            to_tsquery提供了比plainto_tsqueryphraseto_tsquery更多的特性,但是它对其输入要求更加严格。websearch_to_tsqueryto_tsquery的一个简化版本,
            它使用一种可选择的语法,类似于Web搜索引擎使用的语法。


            to_tsquery([ config regconfig, ] querytext text) returns tsquery
            复制


            参考

            http://postgres.cn/docs/12/textsearch-dictionaries.html

            文章转载自CP的PostgreSQL厨房,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

            评论