すべてのプロダクト
Search
ドキュメントセンター

PolarDB:pg_jieba (中国語形態素解析)

最終更新日:Dec 01, 2026

pg_jieba は、中国語テキストの全文検索に使用されるオープンソースのサードパーティ拡張機能です。

適用範囲

次のバージョンの PolarDB for PostgreSQL (Compatible with Oracle) がサポートされています:

  • Oracle 構文互換性 2.0 (マイナーエンジンバージョン 2.0.14.5.2.0 以降)

  • Oracle 構文互換性 1.0 (マイナーエンジンバージョン 2.0.11.9.28.0 以降)

説明

コンソールで、または SHOW polardb_version; ステートメントを実行して、マイナーエンジンバージョンを表示できます。マイナーエンジンバージョンが要件を満たさない場合は、マイナーエンジンバージョンをアップグレードしてください。

概要

pg_jieba 拡張機能では、次のオプションのテキスト検索設定を使用できます:

  • jiebacfg:正確モード。この設定は、冗長な単語を含めずにテキストを正確に形態素解析します。

  • jiebaqry:フルモード。この設定は、照合可能なすべての単語をテキストからスキャンし、冗長な単語が生成される場合があります。

  • jiebacfg_pos:位置情報付きの正確モード。この設定は、正確モードの結果に各単語の位置インデックスを追加し、さらに正確モードで無視されるストップワード (検索で有用ではないほど頻出する単語) も表示します。

使用方法

拡張機能のインストールまたはアンインストール

説明

拡張機能のインストールまたはアンインストールのコマンドを実行できるのは、特権アカウントのみです。

  • 拡張機能をインストールします。

    CREATE EXTENSION pg_jieba;
  • 拡張機能をアンインストールします。

    DROP EXTENSION pg_jieba;

例

例 1

  • 正確モードで jiebacfg を使用します。

    SELECT * FROM to_tsvector('jiebacfg', '小明硕士毕业于中国科学院计算所,后在日本京都大学深造');

    次の結果が返されます:

                                       to_tsvector
    ----------------------------------------------------------------------------------
     '中国科学院':5 '小明':1 '日本京都大学':10 '毕业':3 '深造':11 '硕士':2 '计算所':6
    (1 row)
  • フルモードで jiebaqry を使用します。

    SELECT * FROM to_tsvector('jiebaqry', '小明硕士毕业于中国科学院计算所,后在日本京都大学深造');

    次の結果が返されます:

                                                                               to_tsvector
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------
     '中国':5 '中国科学院':9 '京都':16 '大学':17 '学院':7 '小明':1 '日本':15 '日本京都大学':18 '毕业':3 '深造':19 '硕士':2 '科学':6 '科学院':8 '计算':10 '计算所':11
    (1 row)
  • 位置情報付きの正確モードで jiebacfg_pos を使用します。

    SELECT * FROM to_tsvector('jiebacfg_pos', '小明硕士毕业于中国科学院计算所,后在日本京都大学深造');

    次の結果が返されます:

                                                                   to_tsvector                                                                
    ------------------------------------------------------------------------------------------------------------------------------------------
     '中国科学院:7':5 '于:6':4 '后:16':8 '在:17':9 '小明:0':1 '日本京都大学:18':10 '毕业:4':3 '深造:24':11 '硕士:2':2 '计算所:12':6 ',:15':7
    (1 row)

例 2

  • 正確モードで jiebacfg を使用します。

    SELECT * FROM to_tsvector('jiebacfg', '李小福是创新办主任也是云计算方面的专家');

    次の結果が返されます:

                                to_tsvector
    -------------------------------------------------------------------
     '专家':11 '主任':5 '云计算':8 '创新':3 '办':4 '方面':9 '李小福':1
    (1 row)
  • フルモードで jiebaqry を使用します。

    SELECT * FROM to_tsvector('jiebaqry', '李小福是创新办主任也是云计算方面的专家');

    次の結果が返されます:

                                     to_tsvector
    -----------------------------------------------------------------------------
     '专家':12 '主任':5 '云计算':9 '创新':3 '办':4 '方面':10 '李小福':1 '计算':8
    (1 row)
  • 位置情報付きの正確モードで jiebacfg_pos を使用します。

    SELECT * FROM to_tsvector('jiebacfg_pos', '李小福是创新办主任也是云计算方面的专家');

    次の結果が返されます:

                                                            to_tsvector                                                        
    ---------------------------------------------------------------------------------------------------------------------------
     '专家:17':11 '主任:7':5 '也:9':6 '云计算:11':8 '创新:4':3 '办:6':4 '方面:14':9 '是:10':7 '是:3':2 '李小福:0':1 '的:16':10
    (1 row)

Use pg_jieba with rum for Chinese full-text search

This example uses the rum extension to build a full-text search index for the tokenization results produced by pg_jieba, enabling Chinese full-text search and similarity scoring of matched results. Before you run the example, run CREATE EXTENSION rum; to install the rum extension. For detailed usage of the rum extension, see rum (full-text search acceleration).

  1. Create a table and insert data. Then, use the jiebacfg text search configuration to call the to_tsvector function to write the tokenization results of the text column t into the tsvector-type column v. When you generate the v column, you can perform an update operation or use a trigger.

    CREATE TABLE test_chinese(id serial, t text, d timestamp, v tsvector);
    
    INSERT INTO test_chinese(t,d) VALUES ('南财理财通数据显示,5月公募理财产品吸金榜前十名归属于5家机构,信银理财有5只产品上榜,南银理财占两席,民生理财、恒丰理财、浦银理财各占一席,上榜产品均为中低风险的封闭式固收类产品。从投资周期看,本月榜单产品中有6只产品投资周期为1-2年,3-6个月及6-12个月期限产品各有2只。', '2016-05-01 20:21:22.326724');
    INSERT INTO test_chinese(t,d) VALUES ('其中,信银理财“安盈象固收稳利十四个月封闭式95号”理财产品夺得吸金榜冠军,募集规模达到106.204 亿元;民生理财“富竹固收优选14个月封闭39号”位列第二,募集规模为46.606亿元;信银理财“安盈象固收稳利十四个月封闭式94号”排名第三,募集规模为41.365 亿元。', '2016-05-01 20:21:22.326724');
    INSERT INTO test_chinese(t,d) VALUES ('5月新发产品募集规模最大的是信银理财“安盈象固收稳利十四个月封闭式95号”,实际募集资金高达106.204亿元,该系列产品已蝉联4个月榜首。', '2016-05-01 20:21:22.326724');
    INSERT INTO test_chinese(t,d) VALUES ('南财理财通数据显示,“安盈象固收稳利十四个月封闭式95号”理财产品成立于2024年05月16日,将于2025年07月16日到期,风险评级PR2(中低风险),产品有A、C、D、K类份额,投资周期1-2年,业绩比较基准2.90%~3.45%。', '2016-05-01 20:21:22.326724');
    
    -- Generate a tsvector column using the jiebacfg text search configuration (by performing an update operation or using a trigger)
    UPDATE test_chinese SET v = to_tsvector('jiebacfg', t);
  2. Use the rum_tsvector_ops operator class for column v to create a rum index.

    CREATE INDEX ON test_chinese USING rum (v rum_tsvector_ops);
  3. Query example 1: Query all rows that contain the sample keywords (see the preceding INSERT statement), and use the <=> operator to calculate the similarity between each record and the query keywords.

    SELECT id, v <=> to_tsquery('jiebacfg', '数据 | 理财') as rank
    FROM test_chinese
    where v @@ to_tsquery('jiebacfg', '数据 | 理财');

    The following result is returned:

     id |   rank
    ----+-----------
      1 | 6.6024785
      2 |  12.08523
      3 |  16.44934
      4 |   8.22467
  4. Query example 2: Query all records in column v that contain the sample keyword (see the preceding INSERT statement), and calculate the similarity between each matched record and the sample keyword combination (see the preceding INSERT statement), and return the id and the similarity.

    SELECT id, v <=> to_tsquery('jiebacfg', '数据 | 理财') as rank
    FROM test_chinese
    where v @@ to_tsquery('jiebacfg', '数据');

    The following result is returned:

     id |   rank
    ----+-----------
      1 | 6.6024785
      4 |   8.22467

Extended features

pg_jieba supports configuring and switching between multiple custom dictionaries.

説明

To use a custom dictionary, you must add pg_jieba to the shared_preload_libraries parameter. You can configure the shared_preload_libraries parameter in the console. For instructions, see Configure cluster parameters. Modifying this parameter restarts the cluster. Make service arrangements before you modify the parameter, and proceed with caution.

  1. Insert data into dictionary 0 (dictionary 0 by default, weight 10).

    INSERT INTO jieba_user_dict VALUES ('阿里云');
    INSERT INTO jieba_user_dict VALUES ('研发工程师',0,10);
  2. Tokenize text using the built-in jieba dictionary.

    SELECT * FROM to_tsvector('jiebacfg', 'zth是阿里云的一个研发工程师');

    The following result is returned:

                         to_tsvector
    ------------------------------------------------------
     'zth':1 '一个':6 '云':4 '工程师':8 '研发':7 '阿里':3
    (1 row)
                        
  3. Switch to custom dictionary 0.

    SELECT jieba_load_user_dict(0);

    The following result is returned:

     jieba_load_user_dict
    ----------------------
    
    (1 row)
  4. Tokenize text using dictionary 0.

    SELECT * FROM to_tsvector('jiebacfg', 'zth是阿里云的一个研发工程师');

    The following result is returned:

                    to_tsvector
    --------------------------------------------
     'zth':1 '一个':5 '研发工程师':6 '阿里云':3
    (1 row)