要使用 zhparser,首先需要安装 SCWS 分词库,SCWS 是 Simple Chinese Word Segmentation 的首字母缩写(即:简易中文分词系统)GitHub
- ./configure
- make install
GitHub - amutu/zhparser: zhparser is a PostgreSQL extension for full-text search of Chinese language
make && make install
登录postgresql启用zhparser插件
create extension zhparser;
创建使用zhparser作为解析器的全文搜索的配置
-- 就是需要给zhparser解析器取一个在sql里面可以使用的名称,一下“zh”则为名称 CREATE create text search configuration chinese (parser=zhparser);
添加token映射
alter text search configuration chinese add mapping for a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z with simple;
索引创建
create index idx_name on table_name using gin(to_tsvector('zh', field_name));
查询
SELECT * FROM table_name WHERE to_tsvector('zh', field_name) @@ 'keyword';