Chinese Full-Text Search in PostgreSQL 16
PostgreSQL 16 and PolarDB-PG 16 full-text search can fail to return results for Chinese characters. Learn how to configure the zhparser extension to enable accurate and efficient Chinese FTS.

PostgreSQL 16 and PolarDB-PG 16's full-text search (FTS) can fail to return results when searching for Chinese characters using to_tsvector and to_tsquery. This issue arises because the default text search configuration does not recognize Chinese as a language, treating the entire text as a single token. This can lead to empty search results, even when the search term clearly exists in the text.
Consider the following example: a table of products with Chinese descriptions. When you try to search for '苹果' using FTS, you get no results, even though '苹果' is present in the text. This is because the full-text search parser does not tokenize the text correctly for Chinese.
Understanding the Problem
The primary issue is that PostgreSQL's full-text search is designed for languages that use spaces and punctuation to separate words. Chinese does not use spaces, so the parser treats the entire text as a single token. For example, the query:
SELECT * FROM ts_debug('simple', '这是一款最新款的苹果智能手机,支持5G网络');
returns the entire text as a single token, except for the punctuation. This means that when you use to_tsvector and to_tsquery, the search term '苹果' is not matched because it is not recognized as a separate word.
Using Substring Search as a Workaround
One workaround is to use substring search using the LIKE operator. This approach does not rely on tokenization and can find partial matches within the text. For example:
SELECT id, name, description FROM products WHERE description LIKE '%苹果%';
This query will return the expected results:
-[ RECORD 1 ]-----------------------------------------
id | 1
name | 苹果手机
description | 这是一款最新款的苹果智能手机,支持5G网络
While this method works, it is not ideal for large datasets due to performance concerns. To improve performance, you can create a trigram index:
CREATE EXTENSION IF NOT EXISTS pg_trgm SCHEMA public;
CREATE INDEX products_pattern_idx ON products USING gin (description gin_trgm_ops);
Configuring PostgreSQL for Chinese Full-Text Search
The recommended approach is to configure PostgreSQL to support Chinese full-text search. PolarDB-PG instances come with the zhparser extension, which can be used to create a Chinese-capable FTS configuration. Here is how to set it up:
CREATE EXTENSION IF NOT EXISTS zhparser;
CREATE TEXT SEARCH CONFIGURATION testzhcfg (parser = zhparser);
ALTER TEXT SEARCH CONFIGURATION testzhcfg ADD MAPPING FOR n,v,a,i,e,l WITH simple;
Next, update your table to use the new configuration:
ALTER TABLE products DROP COLUMN search_vector;
CREATE INDEX ON products USING gin (to_tsvector('testzhcfg', description));
Finally, set the default text search configuration for your role:
ALTER ROLE all SET default_text_search_config TO 'testzhcfg';
SET default_text_search_config TO 'testzhcfg';
Testing the Configuration
After setting up the Chinese FTS configuration, you can test it with your query:
SELECT * FROM products WHERE to_tsvector(description) @@ to_tsquery('苹果');
This should now return the correct results:
-[ RECORD 1 ]-----------------------------------------
id | 1
name | 苹果手机
description | 这是一款最新款的苹果智能手机,支持5G网络
The zhparser extension correctly tokenizes the Chinese text, allowing to_tsvector and to_tsquery to function as expected.
Performance Considerations
While the zhparser extension improves full-text search for Chinese, it may introduce additional overhead. The performance impact depends on the size of your dataset and the complexity of your queries. Monitoring and optimizing your database's performance is crucial, especially for large-scale applications.
Consider the following best practices:
- Use appropriate indexing to speed up query execution.
- Optimize your query patterns to reduce the load on the database.
- Regularly analyze and vacuum your tables to maintain database performance.
Where This Approach Does Not Fit
This approach is most suitable for applications that primarily handle Chinese text. If your database contains a mix of languages, you may need to create and manage multiple text search configurations. Additionally, the zhparser extension is specific to PolarDB-PG and may not be available in other PostgreSQL distributions.
For multilingual applications, consider using a more flexible text search solution or a dedicated full-text search engine like Elasticsearch, which supports multiple languages out of the box.
This article draws on PostgreSQL 16 / PolarDB-PG 16 Full-Text Search returns no results when searching Chinese characters with to_tsvector and answers to it (1, 2), on the Stack Exchange network, licensed under CC BY-SA 4.0.
- #chinese
- #full-text-search
- #polardb
- #postgresql
- #to_tsvector
- #zhparser


Comments