PostgreSQL 中文字段全文检索

一、安装 SCWS 中文分词库

wget http://www.xunsearch.com/scws/down/scws-1.2.2.tar.bz2

cd scws-1.2.2
./configure
make && make install

二、安装 zhparser

git clone https://github.com/amutu/zhparser.git

cd zhparser

make && make install

三、安装RUM索引接口,见:https://blog.csdn.net/panguangyuu/article/details/90208787

四、创建扩展

create extension zhparser;
create extension rum;

五、查看需要使用的分词token

select ts_token_type('zhparser');

六、使用zhparser作为解析器的全文搜索的配置

CREATE TEXT SEARCH CONFIGURATION testzhcfg (PARSER = zhparser);

七、往全文搜索配置中增加所需要的token映射

ALTER TEXT SEARCH CONFIGURATION testzhcfg ADD MAPPING FOR n,v,a,i,e,l,j WITH simple;

八、中文分词测试

select to_tsvector('testzhcfg','南京人民政府');

      to_tsvector      
-----------------------
 '人民政府':2 '南京':1
(1 row)

还可以将整行变成一个文本字段,进行分词:

create table Test(
    id serial not null,
    name text,
    name1 text
);

insert into Test(name, name1) values('中国', '南京');

select t::text from Test t;

       t       
---------------
 (1,中国,南京)
(1 row)

-- 将整行数据进行分词操作,查找某行数据存在'中国'的行

select * from (select t::text, t.id from Test t) as sub where to_tsvector(t) @@ '中国'; 

       t       | id 
---------------+----
 (1,中国,南京) |  1
(1 row)

参考文章:https://yq.aliyun.com/articles/636822

你可能感兴趣的:(PostgreSQL)