|
| 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