Skip to content

Instantly share code, notes, and snippets.

@askdkc
Last active June 7, 2023 05:08
Show Gist options
  • Select an option

  • Save askdkc/a3a67f15fcd5e41878958c9a7701f7c7 to your computer and use it in GitHub Desktop.

Select an option

Save askdkc/a3a67f15fcd5e41878958c9a7701f7c7 to your computer and use it in GitHub Desktop.

ひらがな、カタカナ、ローマ字検索実施用インデックス作成テスト

テーブル作成

CREATE EXTENSION IF NOT EXISTS pgroonga;

CREATE TABLE memos (
  id integer,
  title text,
  content text
);

データ追加とインデックス作成

INSERT INTO memos VALUES (1, 'PostgreSQLはリレーショナル・データベース管理システムです。','すごいでしょう');
INSERT INTO memos VALUES (2, 'Groongaは日本語対応の高速な全文検索エンジンです。','スワイショウ');
INSERT INTO memos VALUES (3, 'PGroongaはインデックスとしてGroongaを使うためのPostgreSQLの拡張機能です。','ハバナイスデー');
INSERT INTO memos VALUES (4, 'groongaコマンドがあります。','今日はコンバンワこのくにわ');

CREATE INDEX pgroonga_title_search_index ON memos USING pgroonga (title) 
  WITH (
    normalizers = 'NormalizerNFKC150("unify_to_romaji", true, "unify_hyphen_and_prolonged_sound_mark", true)',
    tokenizer = 'TokenBigramSplitSymbolAlphaDigit',
    token_filters = 'TokenFilterNFKC150("unify_to_romaji", true, "unify_kana", true, "unify_hyphen_and_prolonged_sound_mark", true)'
  );
  
CREATE INDEX pgroonga_content_search_index ON memos USING pgroonga (content) 
  WITH (
    normalizers = 'NormalizerNFKC150("unify_to_romaji", true, "unify_hyphen_and_prolonged_sound_mark", true)',
    tokenizer = 'TokenBigramSplitSymbolAlphaDigit',
    token_filters = 'TokenFilterNFKC150("unify_to_romaji", true, "unify_kana", true, "unify_hyphen_and_prolonged_sound_mark", true)'
  );

検索クエリーのテスト

select * from memos where array[title,content] &@~ '';
 id |                                   title                                   |    content     
----+---------------------------------------------------------------------------+----------------
  1 | PostgreSQLはリレーショナル・データベース管理システムです。                | すごいでしょう
  2 | Groongaは日本語対応の高速な全文検索エンジンです。                         | スワイショウ
  3 | PGroongaはインデックスとしてGroongaを使うためのPostgreSQLの拡張機能です。 | ハバナイスデー
(3 rows)

-- YEY!!
select * from memos where title &@~ 'de-ta';
 id |                           title                            |    content     
----+------------------------------------------------------------+----------------
  1 | PostgreSQLはリレーショナル・データベース管理システムです。 | すごいでしょう
(1 row)

-- YEY!!
select * from memos where title &@~ 'りれーしょなる';
 id |                           title                            |    content     
----+------------------------------------------------------------+----------------
  1 | PostgreSQLはリレーショナル・データベース管理システムです。 | すごいでしょう
(1 row)

-- YEY!!
select * from memos where title &@~ 'rire-shonaru';
 id |                           title                            |    content     
----+------------------------------------------------------------+----------------
  1 | PostgreSQLはリレーショナル・データベース管理システムです。 | すごいでしょう
(1 row)


-- WHY??
select * from memos where array[title,content] &@~ 'de-ta';
 id | title | content 
----+-------+---------
(0 rows)

-- WHY??
select * from memos where array[title,content] &@~ 'rire-shonaru';
 id | title | content 
----+-------+---------
(0 rows)

-- WHY??
select * from memos where array[title,content] &@~ 'りれーしょなる';
 id | title | content 
----+-------+---------
(0 rows)

インデックス指定しないと上手く動かない

CREATE INDEX pgroonga_search_index ON memos USING pgroonga (title,content) 
  WITH (
    normalizers = 'NormalizerNFKC150("unify_to_romaji", true, "unify_hyphen_and_prolonged_sound_mark", true)',
    tokenizer = 'TokenBigramSplitSymbolAlphaDigit',
    token_filters = 'TokenFilterNFKC150("unify_to_romaji", true, "unify_kana", true, "unify_hyphen_and_prolonged_sound_mark", true)'
  );

-- Works!!
select * from memos where array[title,content] &@~ ('rire-shonaru', ARRAY[1,1], 'pgroonga_search_index')::pgroonga_full_text_search_condition;
 id |                           title                            |    content     
----+------------------------------------------------------------+----------------
  1 | PostgreSQLはリレーショナル・データベース管理システムです。 | すごいでしょう
(1 row)

-- Works!!
select * from memos where array[title,content] &@~ ('rire-shonaru', NULL, 'pgroonga_search_index')::pgroonga_full_text_search_condition;
 id |                           title                            |    content     
----+------------------------------------------------------------+----------------
  1 | PostgreSQLはリレーショナル・データベース管理システムです。 | すごいでしょう
(1 row)

-- Not Working!?
select * from memos where array[title,content] &@~ ('rire-shonaru', ARRAY[1,1], NULL)::pgroonga_full_text_search_condition;
 id | title | content 
----+-------+---------
(0 rows)

実はインデックス作成時に配列(Array)インデックスにしないと動かない

drop index pgroonga_search_index

CREATE INDEX pgroonga_search_index ON memos USING pgroonga ((ARRAY[title,content]))                                                              
  WITH (
    normalizers = 'NormalizerNFKC150("unify_to_romaji", true, "unify_hyphen_and_prolonged_sound_mark", true)',
    tokenizer = 'TokenBigramSplitSymbolAlphaDigit',
    token_filters = 'TokenFilterNFKC150("unify_to_romaji", true, "unify_kana", true, "unify_hyphen_and_prolonged_sound_mark", true)'
);


-- Yey!!
select * from memos where array[title,content] &@~ ('rire-shonaru', NULL, NULL)::pgroonga_full_text_search_condition;
 id |                           title                            |    content     
----+------------------------------------------------------------+----------------
  1 | PostgreSQLはリレーショナル・データベース管理システムです。 | すごいでしょう
(1 row)


-- Yey!
select * from memos where array[title,content] &@~ 'rire-shonaru';
 id |                           title                            |    content     
----+------------------------------------------------------------+----------------
  1 | PostgreSQLはリレーショナル・データベース管理システムです。 | すごいでしょう
(1 row)

トークンフィルターは要らないみたい

DROP INDEX pgroonga_search_index;

CREATE INDEX pgroonga_search_index ON memos USING pgroonga ((ARRAY[title,content])) 
  WITH (
    normalizers = 'NormalizerNFKC150("unify_to_romaji", true, "unify_hyphen_and_prolonged_sound_mark", true)',
    tokenizer = 'TokenBigramSplitSymbolAlphaDigit' 
  ); 
  
select * from memos where array[title,content] &@~ 'rire-shonaru';
 id |                           title                            |    content     
----+------------------------------------------------------------+----------------
  1 | PostgreSQLはリレーショナル・データベース管理システムです。 | すごいでしょう
(1 row)

select * from memos where ARRAY[title,content] &@~ 'スゴイ';
 id |                           title                            |    content     
----+------------------------------------------------------------+----------------
  1 | PostgreSQLはリレーショナル・データベース管理システムです。 | すごいでしょう
(1 row)

select * from memos where ARRAY[title,content] &@~ 'sugoi';
 id |                           title                            |    content     
----+------------------------------------------------------------+----------------
  1 | PostgreSQLはリレーショナル・データベース管理システムです。 | すごいでしょう
(1 row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment