Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CN/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
*** xref:master/ecosystem_components/wal2json.adoc[wal2json]
*** xref:master/ecosystem_components/pg_stat_monitor.adoc[pg_stat_monitor]
*** xref:master/ecosystem_components/pg_ai_query.adoc[pg_ai_query]
*** xref:master/ecosystem_components/pg_textsearch.adoc[pg_textsearch]
* 监控运维
** xref:master/getting-started/daily_monitoring.adoc[日常监控]
** xref:master/getting-started/daily_maintenance.adoc[日常维护]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

:sectnums:
:sectnumlevels: 5

= pg_textsearch

== 概述
pg_textsearch 是由Timescale团队开发的一个PostgreSQL扩展,旨在为Postgres提供高性能、现代化的全文搜索能力,并针对AI工作负载和混合搜索进行了优化。

== 安装

[TIP]
源码安装环境为 Ubuntu 24.04(x86_64),环境中已经安装了IvorySQL5及以上版本,安装路径为/usr/ivory-5

=== 源码安装

[literal]
----
# 从 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

----

[TIP]
如果出现找不到xlocale.h的错误,需要手动修改 /usr/ivory-5/include/postgresql/server/pg_config.h
删除或者注释掉 #define HAVE_XLOCALE_H 1 这一行

=== 修改数据库配置文件

修改 ivorysql.conf 文件,添加 pg_textsearch 到 shared_preload_libraries

[literal]
----
shared_preload_libraries = 'gb18030_2022, liboracle_parser, ivorysql_ora, pg_textsearch'
----

重启数据库后配置生效。

=== 创建Extension

[literal]
----
postgres=# create extension pg_textsearch;
WARNING: pg_textsearch v0.6.1 is a prerelease. Do not use in production.
CREATE EXTENSION
----

== 使用

创建一个带有文本内容的表:
[literal]
----
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 索引:
[literal]
----
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
----

使用@操作符获取最相关的文档:
[literal]
----
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)
----

1 change: 1 addition & 0 deletions EN/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
*** xref:master/ecosystem_components/pg_ai_query.adoc[pg_ai_query]
*** xref:master/ecosystem_components/wal2json.adoc[wal2json]
*** xref:master/ecosystem_components/pg_stat_monitor.adoc[pg_stat_monitor]
*** xref:master/ecosystem_components/pg_textsearch.adoc[pg_textsearch]
* Monitor and O&M
** xref:master/getting-started/daily_monitoring.adoc[Monitoring]
** xref:master/getting-started/daily_maintenance.adoc[Maintenance]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@

:sectnums:
:sectnumlevels: 5

= pg_textsearch

== Overview
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.

== Installation

[TIP]
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.

=== Source Code Installation

[literal]
----
# download source code package from: 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

# compile and install the extension
make PG_CONFIG=/usr/ivory-5/bin/pg_config
make PG_CONFIG=/usr/ivory-5/bin/pg_config install
----

[TIP]
If there is error "xlocale.h: No such file or directory" during compilation, user should remove the
line of "#define HAVE_XLOCALE_H 1" from file /usr/ivory-5/include/postgresql/server/pg_config.h.

=== Modify the configuration file

Modify the ivorysql.conf file to add pg_textsearch into shared_preload_libraries.
[literal]
----
shared_preload_libraries = 'gb18030_2022, liboracle_parser, ivorysql_ora, pg_textsearch'
----

Then restart the database.

=== Create Extension

[literal]
----
postgres=# create extension pg_textsearch;
WARNING: pg_textsearch v0.6.1 is a prerelease. Do not use in production.
CREATE EXTENSION
----

== Use

Create a table with text content:
[literal]
----
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
----

Create a pg_textsearch index on the text column:
[literal]
----
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
----

Get the most relevant documents using the <@> operator:
[literal]
----
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)
----
Loading