The pg_jieba extension brings Jieba's Chinese word segmentation into AnalyticDB for PostgreSQL, enabling efficient Chinese full-text search on distributed data.
Prerequisites
Before you begin, make sure that:
The AnalyticDB for PostgreSQL instance is in elastic storage mode
The minor version meets the minimum requirement:
AnalyticDB for PostgreSQL V6.0: 6.6.2.1 or later
AnalyticDB for PostgreSQL V7.0: 7.0.5 or later
To check your minor version, see View the minor engine version.
Install pg_jieba
On the Extensions page of the AnalyticDB for PostgreSQL console, install the pg_jieba extension. For details, see Install, update, and uninstall extensions.
Switch to the public schema of the target database and run the following statement to verify the installation:
SELECT * FROM pg_extension WHERE extname = 'pg_jieba';If the extension is installed, the output looks like this:
+--------+--------+--------+--------+ |oid |extname |extowner|... | +--------+--------+--------+--------+ |17194 |pg_jieba|10. |... | +--------+--------+--------+--------+If no rows are returned, the extension is not installed in the public schema of that database.
Perform Chinese word segmentation
pg_jieba registers the jiebacfg text search configuration. Pass jiebacfg as the first argument to to_tsvector or to_tsquery to segment Chinese text.
Segment text into a tsvector:
SELECT to_tsvector('jiebacfg', '有两种方法进行全文检索');Result:
+---------------------------------------+
| to_tsvector |
+---------------------------------------+
|'两种':2 '全文检索':5 '方法':3 '进行':4 |
+---------------------------------------+
(1 row)Run a full-text search query:
SELECT to_tsvector('jiebacfg', '有两种方法进行全文检索') @@ to_tsquery('jiebacfg', '全文检索');Result:
+----------+
| ?column? |
+----------+
| t |
+----------+
(1 row)Use a custom dictionary
By default, pg_jieba uses its built-in dictionary. For domain-specific terms—product names, technical jargon, or compound phrases—add them to the custom dictionary so they are treated as single tokens rather than split into parts.
When pg_jieba is installed, it automatically creates the jieba.jieba_custom_word table with the following schema:
CREATE TABLE jieba.jieba_custom_word
(
word text primary key, -- Custom word
weight float8 default '1.0', -- Weight
type text default 'x' -- Part of speech
);Apply for access
Submit a ticket to apply for write permissions on the jieba.jieba_custom_word table.
Manage custom words
Add a word:
INSERT INTO jieba.jieba_custom_word values('两种方法');Remove a word:
DELETE FROM jieba.jieba_custom_word WHERE word='两种方法';Query the table:
SELECT * FROM jieba.jieba_custom_word;Reload the dictionary
After adding or removing words, reload the dictionary for the changes to take effect. Without this step, segmentation results remain unchanged.
SELECT jieba.jieba_load_user_dict();Verify segmentation results
Run the same query before and after reloading to confirm the effect:
SELECT to_tsvector('jiebacfg', '有两种方法进行全文检索');| Scenario | Result |
|---|---|
| Before custom dictionary | '两种':2 '全文检索':5 '方法':3 '进行':4 |
After adding 两种方法 and reloading | '两种方法':2 '全文检索':4 '进行':3 |
After the custom dictionary is loaded, 两种方法 is treated as a single token instead of being split into 两种 and 方法.
What's next
Full-Text Search — PostgreSQL full-text search reference
Text Search Functions and Operators — Reference for
to_tsvector,to_tsquery, and related functions