Skip to content

Commit 1546941

Browse files
committed
support pg_textsearch
1 parent c95a0a7 commit 1546941

File tree

4 files changed

+181
-0
lines changed

4 files changed

+181
-0
lines changed

CN/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
*** xref:master/ecosystem_components/wal2json.adoc[wal2json]
5454
*** xref:master/ecosystem_components/pg_stat_monitor.adoc[pg_stat_monitor]
5555
*** xref:master/ecosystem_components/pg_ai_query.adoc[pg_ai_query]
56+
*** xref:master/ecosystem_components/pg_textsearch.adoc[pg_textsearch]
5657
* 监控运维
5758
** xref:master/getting-started/daily_monitoring.adoc[日常监控]
5859
** xref:master/getting-started/daily_maintenance.adoc[日常维护]
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
:sectnums:
3+
:sectnumlevels: 5
4+
5+
= pg_textsearch
6+
7+
== 概述
8+
pg_textsearch 是由Timescale团队开发的一个PostgreSQL扩展,旨在为Postgres提供高性能、现代化的全文搜索能力,并针对AI工作负载和混合搜索进行了优化。
9+
10+
== 安装
11+
12+
[TIP]
13+
源码安装环境为 Ubuntu 24.04(x86_64),环境中已经安装了IvorySQL5及以上版本,安装路径为/usr/ivory-5
14+
15+
=== 源码安装
16+
17+
[literal]
18+
----
19+
# 从 wget https://github.com/timescale/pg_textsearch/archive/refs/tags/v0.6.1.tar.gz 下载源码包
20+
21+
tar xzvf v0.6.1.tar.gz
22+
cd pg_textsearch-0.6.1
23+
24+
# 编译安装插件
25+
make PG_CONFIG=/usr/ivory-5/bin/pg_config
26+
make PG_CONFIG=/usr/ivory-5/bin/pg_config install
27+
28+
----
29+
30+
[TIP]
31+
如果出现找不到xlocale.h的错误,需要手动修改 /usr/ivory-5/include/postgresql/server/pg_config.h
32+
删除或者注释掉 #define HAVE_XLOCALE_H 1 这一行
33+
34+
=== 修改数据库配置文件
35+
36+
修改 ivorysql.conf 文件,添加 pg_textsearch 到 shared_preload_libraries
37+
38+
[literal]
39+
----
40+
shared_preload_libraries = 'gb18030_2022, liboracle_parser, ivorysql_ora, pg_textsearch'
41+
----
42+
43+
重启数据库后配置生效。
44+
45+
=== 创建Extension
46+
47+
[literal]
48+
----
49+
postgres=# create extension pg_textsearch;
50+
WARNING: pg_textsearch v0.6.1 is a prerelease. Do not use in production.
51+
CREATE EXTENSION
52+
----
53+
54+
== 使用
55+
56+
创建一个带有文本内容的表:
57+
[literal]
58+
----
59+
postgres=# CREATE TABLE documents (id bigserial PRIMARY KEY, content text);
60+
CREATE TABLE
61+
62+
postgres=# INSERT INTO documents (content) VALUES
63+
('PostgreSQL is a powerful database system'),
64+
('BM25 is an effective ranking function'),
65+
('Full text search with custom scoring');
66+
INSERT 0 3
67+
----
68+
69+
在文本列上创建 pg_textsearch 索引
70+
[literal]
71+
----
72+
postgres=# CREATE INDEX docs_idx ON documents USING bm25(content) WITH (text_config='english');
73+
NOTICE: BM25 index build started for relation docs_idx
74+
NOTICE: Using text search configuration: english
75+
NOTICE: Using index options: k1=1.20, b=0.75
76+
NOTICE: BM25 index build completed: 3 documents, avg_length=4.33
77+
CREATE INDEX
78+
----
79+
80+
使用@操作符获取最相关的文档:
81+
[literal]
82+
----
83+
postgres=# SELECT * FROM documents ORDER BY content <@> 'database system' LIMIT 5;
84+
id | content
85+
----+------------------------------------------
86+
1 | PostgreSQL is a powerful database system
87+
2 | BM25 is an effective ranking function
88+
3 | Full text search with custom scoring
89+
(3 rows)
90+
----
91+

EN/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
*** xref:master/ecosystem_components/pg_ai_query.adoc[pg_ai_query]
5454
*** xref:master/ecosystem_components/wal2json.adoc[wal2json]
5555
*** xref:master/ecosystem_components/pg_stat_monitor.adoc[pg_stat_monitor]
56+
*** xref:master/ecosystem_components/pg_textsearch.adoc[pg_textsearch]
5657
* Monitor and O&M
5758
** xref:master/getting-started/daily_monitoring.adoc[Monitoring]
5859
** xref:master/getting-started/daily_maintenance.adoc[Maintenance]
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
2+
:sectnums:
3+
:sectnumlevels: 5
4+
5+
= pg_textsearch
6+
7+
== Overview
8+
pg_textsearch is a PostgreSQL extension developed by the Timescale team. It is designed to provide high-performance, modern full-text search capabilities for Postgres, and is optimized for AI workloads and hybrid search.
9+
10+
== Installation
11+
12+
[TIP]
13+
The source code installation environment is Ubuntu 24.04 (x86_64), in which IvorySQL 5 or a later version has been installed. The installation path is /usr/ivory-5.
14+
15+
=== Source Code Installation
16+
17+
[literal]
18+
----
19+
# download source code package from: https://github.com/timescale/pg_textsearch/archive/refs/tags/v0.6.1.tar.gz
20+
21+
tar xzvf v0.6.1.tar.gz
22+
cd pg_textsearch-0.6.1
23+
24+
# compile and install the extension
25+
make PG_CONFIG=/usr/ivory-5/bin/pg_config
26+
make PG_CONFIG=/usr/ivory-5/bin/pg_config install
27+
----
28+
29+
[TIP]
30+
If there is error "xlocale.h: No such file or directory" during compilation, user should remove the
31+
line of "#define HAVE_XLOCALE_H 1" from file /usr/ivory-5/include/postgresql/server/pg_config.h.
32+
33+
=== Modify the configuration file
34+
35+
Modify the ivorysql.conf file to add pg_textsearch into shared_preload_libraries.
36+
[literal]
37+
----
38+
shared_preload_libraries = 'gb18030_2022, liboracle_parser, ivorysql_ora, pg_textsearch'
39+
----
40+
41+
Then restart the database.
42+
43+
=== Create Extension
44+
45+
[literal]
46+
----
47+
postgres=# create extension pg_textsearch;
48+
WARNING: pg_textsearch v0.6.1 is a prerelease. Do not use in production.
49+
CREATE EXTENSION
50+
----
51+
52+
== Use
53+
54+
Create a table with text content:
55+
[literal]
56+
----
57+
postgres=# CREATE TABLE documents (id bigserial PRIMARY KEY, content text);
58+
CREATE TABLE
59+
60+
postgres=# INSERT INTO documents (content) VALUES
61+
('PostgreSQL is a powerful database system'),
62+
('BM25 is an effective ranking function'),
63+
('Full text search with custom scoring');
64+
INSERT 0 3
65+
----
66+
67+
Create a pg_textsearch index on the text column:
68+
[literal]
69+
----
70+
postgres=# CREATE INDEX docs_idx ON documents USING bm25(content) WITH (text_config='english');
71+
NOTICE: BM25 index build started for relation docs_idx
72+
NOTICE: Using text search configuration: english
73+
NOTICE: Using index options: k1=1.20, b=0.75
74+
NOTICE: BM25 index build completed: 3 documents, avg_length=4.33
75+
CREATE INDEX
76+
----
77+
78+
Get the most relevant documents using the <@> operator:
79+
[literal]
80+
----
81+
postgres=# SELECT * FROM documents ORDER BY content <@> 'database system' LIMIT 5;
82+
id | content
83+
----+------------------------------------------
84+
1 | PostgreSQL is a powerful database system
85+
2 | BM25 is an effective ranking function
86+
3 | Full text search with custom scoring
87+
(3 rows)
88+
----

0 commit comments

Comments
 (0)