PostgreSQL8.3.X新特性-全文搜索(五)

来源:互联网 发布:js获取单选框选中状态 编辑:程序博客网 时间:2024/04/30 04:26

 PostgreSQL8.3.X新特性-全文搜索(五)

<script type="text/javascript"><!--google_ad_client = "pub-5873492303276472";/* 728x90, 创建于 08-7-29 */google_ad_slot = "7502041044";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

 

配置举例

一个文本搜索配置需要声明把一个文档转换成 tsvector 的所有必要的选项:用于把文本分析成记号的分词程序(分析器),和用于把所有记号转换成语意的词典。每次调用 to_tsvector或者 to_tsquery 都需要一个文本搜索配置来执行其处理。配置参数 default_text_search_config 声明缺省的配置的名称,如果我们忽略了明确的配置名参数,那么就会用做缺省。我们可以在 postgresql.conf 内设置,或者用 SET 命令为每个独立的会话设置。

几个预定义的文本搜索配置是可用的,而且你可以很容易地创建客户化的配置。要实对文本搜索对象的管理,我们有一个 SQL 命令集可以实用,有好几个 psql 命令显示有关文本搜索对象的信息(第12.10节)。

比如,我们可以创建一个配置 pg,从内置的 english 配置开始。

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

我们将实用 PostgreSQL 相关的同义词列表,并且把它存储在 $SHAREDIR/tsearch_data/pg_dict.syn 里头。文件的内容看上去类似:

  postgres    pg  pgsql       pg  postgresql  pg

我们用下面这样的命令定义同义词词典:

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

然后我们注册 Ispell 词典 english_ispell,它有自己的配置文件:

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

现在我们可以为配置 pg 里面的单词设置映射:

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

我们选择不索引或者不搜索某些内置的配置可以处理的记号类型:

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

现在我们测试我们的配置:

SELECT * FROM ts_debug('public.pg', 'PostgreSQL, the highly scalable, SQL compliant, open source object-relationaldatabase management system, is now undergoing beta testing of the nextversion of our software.');

下一步是设置会话,让它使用新的配置,它是在 public 模式里创建的:

=> /dF   List of text search configurations Schema  | Name | Description---------+------+------------- public  | pg   |SET default_text_search_config = 'public.pg';SETSHOW default_text_search_config; default_text_search_config---------------------------- public.pg
<script type="text/javascript"><!--google_ad_client = "pub-5873492303276472";/* 728x15, 创建于 08-7-29 */google_ad_slot = "7630759450";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>