PostgreSQL 配置例子

2021-08-27 14:57 更新

一個(gè)文本搜索配置指定了將一個(gè)文檔轉(zhuǎn)換成一個(gè)tsvector所需的所有選項(xiàng):用于把文本分解成記號(hào)的解析器,以及用于將每一個(gè)記號(hào)轉(zhuǎn)換成詞位的詞典。每一次to_tsvectorto_tsquery的調(diào)用都需要一個(gè)文本搜索配置來執(zhí)行其處理。配置參數(shù)default_text_search_config指定了默認(rèn)配置的名稱,如果忽略了顯式的配置參數(shù),文本搜索函數(shù)將會(huì)使用它。它可以在 postgresql.conf中設(shè)置,或者使用SET命令為一個(gè)單獨(dú)的會(huì)話設(shè)置。

有一些預(yù)定義的文本搜索配置可用,并且你可以容易地創(chuàng)建自定義的配置。為了便于管理文本搜索對(duì)象,可以使用一組SQL命令,并且有多個(gè)psql命令可以顯示有關(guān)文本搜索對(duì)象(第 12.10 節(jié))的信息。

作為一個(gè)例子,我們將創(chuàng)建一個(gè)配置pg,從復(fù)制內(nèi)建的english配置開始:

CREATE TEXT SEARCH CONFIGURATION public.pg ( COPY = pg_catalog.english );

我們將使用一個(gè) PostgreSQL 相關(guān)的同義詞列表,并將它存儲(chǔ)在$SHAREDIR/tsearch_data/pg_dict.syn中。文件內(nèi)容看起來像:

postgres    pg
pgsql       pg
postgresql  pg

我們定義同義詞詞典如下:

CREATE TEXT SEARCH DICTIONARY pg_dict (
    TEMPLATE = synonym,
    SYNONYMS = pg_dict
);

接下來我們注冊(cè)Ispell詞典english_ispell,它有其自己的配置文件:

CREATE TEXT SEARCH DICTIONARY english_ispell (
    TEMPLATE = ispell,
    DictFile = english,
    AffFile = english,
    StopWords = english
);

現(xiàn)在我們可以在配置pg中建立詞的映射:

ALTER TEXT SEARCH CONFIGURATION pg
    ALTER MAPPING FOR asciiword, asciihword, hword_asciipart,
                      word, hword, hword_part
    WITH pg_dict, english_ispell, english_stem;

我們選擇不索引或搜索某些內(nèi)建配置確實(shí)處理的記號(hào)類型:

ALTER TEXT SEARCH CONFIGURATION pg
    DROP MAPPING FOR email, url, url_path, sfloat, float;

現(xiàn)在我們可以測(cè)試我們的配置:

SELECT * FROM ts_debug('public.pg', '
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.
');

下一個(gè)步驟是設(shè)置會(huì)話讓它使用新配置,它被創(chuàng)建在public模式中:

=> \dF
   List of text search configurations
 Schema  | Name | Description
---------+------+-------------
 public  | pg   |

SET default_text_search_config = 'public.pg';
SET

SHOW default_text_search_config;
 default_text_search_config
----------------------------
 public.pg


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)