本文帮助您快速理解PolarDB PostgreSQL版全文检索的基本用法,包括安装/卸载插件、创建表、创建索引和查询等操作。
安装插件
CREATE EXTENSION rum;如需使用中文分词功能,可同步安装中文分词插件。
CREATE EXTENSION pg_jieba;说明 
以上插件的详细版本限制请参见pg_jieba(中文分词)和rum(全文检索加速)。
创建表
- 创建英文表。 - CREATE TABLE test_english(id serial, t text, d timestamp);
- 创建中文表。 - CREATE TABLE test_chinese(id serial, t text, d timestamp, v tsvector);
插入数据
- 向英文表插入测试数据。 - INSERT INTO test_english(t,d) VALUES ('The situation is most beautiful', '2016-05-01 20:21:22.326724'); INSERT INTO test_english(t,d) VALUES ('It is a beautiful', '2016-05-01 21:21:22.326724'); INSERT INTO test_english(t,d) VALUES ('It looks like a beautiful place', '2016-05-01 22:21:22.326724'); INSERT INTO test_english(t,d) VALUES ('It looks l a beautiful place', '2016-05-02 00:21:22.326724'); INSERT INTO test_english(t,d) VALUES ('It looks like a beautiful places', '2016-05-02 02:21:22.326724'); INSERT INTO test_english(t,d) VALUES ('It looks like a beaut places', '2016-05-02 03:21:22.326724'); INSERT INTO test_english(t,d) VALUES ('It looks like a pig', '2016-05-02 03:21:22.326724');
- 向中文表插入测试数据。 - INSERT INTO test_chinese(t,d) VALUES ('南财理财通数据显示,5月公募理财产品吸金榜前十名归属于5家机构,信银理财有5只产品上榜,南银理财占两席,民生理财、恒丰理财、浦银理财各占一席,上榜产品均为中低风险的封闭式固收类产品。从投资周期看,本月榜单产品中有6只产品投资周期为1-2年,3-6个月及6-12个月期限产品各有2只。', '2016-05-01 20:21:22.326724'); INSERT INTO test_chinese(t,d) VALUES ('其中,信银理财“安盈象固收稳利十四个月封闭式95号”理财产品夺得吸金榜冠军,募集规模达到106.204 亿元;民生理财“富竹固收优选14个月封闭39号”位列第二,募集规模为46.606亿元;信银理财“安盈象固收稳利十四个月封闭式94号”排名第三,募集规模为41.365 亿元。', '2016-05-01 20:21:22.326724'); INSERT INTO test_chinese(t,d) VALUES ('5月新发产品募集规模最大的是信银理财“安盈象固收稳利十四个月封闭式95号”,实际募集资金高达106.204亿元,该系列产品已蝉联4个月榜首。', '2016-05-01 20:21:22.326724'); INSERT INTO test_chinese(t,d) VALUES ('南财理财通数据显示,“安盈象固收稳利十四个月封闭式95号”理财产品成立于2024年05月16日,将于2025年07月16日到期,风险评级PR2(中低风险),产品有A、C、D、K类份额,投资周期1-2年,业绩比较基准2.90%~3.45%。', '2016-05-01 20:21:22.326724'); -- 该操作可以通过执行更新操作或使用触发器的方式进行 UPDATE test_chinese SET v = to_tsvector('jiebacfg', t);
创建索引
- 为英文表创建索引。 - CREATE INDEX ON test_english USING rum (t rum_text_ops);
- 为中文表创建索引。 - CREATE INDEX ON test_chinese USING rum (v rum_tsvector_ops);
查询
- 英文表查询。 - 查询表中包含 - beautiful或- place关键字的所有行:- SELECT * FROM test_english WHERE to_tsvector('english', t) @@ to_tsquery('english', 'beautiful | place');- 返回结果如下: - id | t | d ----+----------------------------------+---------------------------- 1 | The situation is most beautiful | 2016-05-01 20:21:22.326724 2 | It is a beautiful | 2016-05-01 21:21:22.326724 3 | It looks like a beautiful place | 2016-05-01 22:21:22.326724 4 | It looks l a beautiful place | 2016-05-02 00:21:22.326724 5 | It looks like a beautiful places | 2016-05-02 02:21:22.326724 6 | It looks like a beaut places | 2016-05-02 03:21:22.326724
- 查询表中包含 - beautiful或- place关键字的所有行,同时计算出每条记录与查询关键字之间的相似度,并按相似度降序排列。- SELECT *, to_tsvector('english', t) <=> to_tsquery('english', 'beautiful | place') AS rank FROM test_english WHERE to_tsvector('english', t) @@ to_tsquery('english', 'beautiful | place') ORDER BY to_tsvector('english', t) <=> to_tsquery('english', 'beautiful | place') DESC;- 返回结果如下: - id | t | d | rank ----+----------------------------------+----------------------------+--------- 1 | The situation is most beautiful | 2016-05-01 20:21:22.326724 | 16.4493 2 | It is a beautiful | 2016-05-01 21:21:22.326724 | 16.4493 6 | It looks like a beaut places | 2016-05-02 03:21:22.326724 | 16.4493 3 | It looks like a beautiful place | 2016-05-01 22:21:22.326724 | 8.22467 4 | It looks l a beautiful place | 2016-05-02 00:21:22.326724 | 8.22467 5 | It looks like a beautiful places | 2016-05-02 02:21:22.326724 | 8.22467 (6 rows)
 
- 中文表查询。 - 查询表中包含 - 数据或- 理财关键字的所有行:- SELECT id, v <=> to_tsquery('jiebacfg', '数据 | 理财') as rank FROM test_chinese where v @@ to_tsquery('jiebacfg', '数据 | 理财');- 返回结果如下: - id | rank ----+----------- 1 | 6.6024785 2 | 12.08523 3 | 16.44934 4 | 8.22467
- 查询列 - v中包含- 数据的所有记录,计算每个匹配记录与- '数据 | 理财'之间的相似度,返回- id和相似度。- SELECT id, v <=> to_tsquery('jiebacfg', '数据 | 理财') as rank FROM test_chinese where v @@ to_tsquery('jiebacfg', '数据');- 返回结果如下: - id | rank ----+----------- 1 | 6.6024785 4 | 8.22467
 
卸载插件(可选)
DROP EXTENSION rum CASCADE;
DROP EXTENSION pg_jieba CASCADE;该文章对您有帮助吗?