pg_textsearch
2. 安装
| 源码安装环境为 Ubuntu 24.04(x86_64),环境中已经安装了IvorySQL5及以上版本,安装路径为/usr/ivory-5 |
2.1. 源码安装
# 从 https://github.com/timescale/pg_textsearch/archive/refs/tags/v0.6.1.tar.gz 下载源码包 tar xzvf v0.6.1.tar.gz cd pg_textsearch-0.6.1 # 编译安装插件 make PG_CONFIG=/usr/ivory-5/bin/pg_config make PG_CONFIG=/usr/ivory-5/bin/pg_config install
| 如果出现找不到xlocale.h的错误,需要手动修改 /usr/ivory-5/include/postgresql/server/pg_config.h 删除或者注释掉 #define HAVE_XLOCALE_H 1 这一行 |
3. 使用
创建一个带有文本内容的表:
postgres=# CREATE TABLE documents (id bigserial PRIMARY KEY, content text);
CREATE TABLE
postgres=# INSERT INTO documents (content) VALUES
('PostgreSQL is a powerful database system'),
('BM25 is an effective ranking function'),
('Full text search with custom scoring');
INSERT 0 3
在文本列上创建 pg_textsearch 索引:
postgres=# CREATE INDEX docs_idx ON documents USING bm25(content) WITH (text_config='english'); NOTICE: BM25 index build started for relation docs_idx NOTICE: Using text search configuration: english NOTICE: Using index options: k1=1.20, b=0.75 NOTICE: BM25 index build completed: 3 documents, avg_length=4.33 CREATE INDEX
使用@操作符获取最相关的文档:
postgres=# SELECT * FROM documents ORDER BY content <@> 'database system' LIMIT 5; id | content ----+------------------------------------------ 1 | PostgreSQL is a powerful database system 2 | BM25 is an effective ranking function 3 | Full text search with custom scoring (3 rows)