groonga - An open-source fulltext search engine and column store.

4.7. match_columnsパラメータ

4.7.1. 複数のカラムを対象とした全文検索

groongaでは、複数のカラムを対象とした全文検索を行うことができます。例えば、ブログのテーブルで、タイトルと内容とがそれぞれ別のカラムに入ったものがあるとしましょう。「タイトルもしくは内容に特定の単語を含む」検索を行いたいとします。

この場合、2つのインデックス作成方式があります。1つは、それぞれのカラムに1つずつインデックスを付与する方式です。もう1つは、複数のカラムに対して1つのインデックスを付与する方式です。groongaでは、どちらの形式のインデックスが存在している場合でも、同一の記法で全文検索を行うことができます。

4.7.1.1. カラムごとにインデックスを付与する場合

Blog1テーブルを作り、タイトル文字列のtitleカラム、本文のmessageカラムを追加しています。 インデックス用のIndexBlog1テーブルも作り、titleカラムのインデックス用にindex_titleカラム、messageカラムのインデック用にindex_messageカラムと、それぞれ1カラムごとに1つずつ追加しています。

実行例

> table_create --name Blog1 --flags TABLE_HASH_KEY --key_type ShortText
[[0,1294292405.5139,0.046640501],true]
> column_create --table Blog1 --name title --flags COLUMN_SCALAR --type ShortText
[[0,1294292405.76135,0.05035077],true]
> column_create --table Blog1 --name message --flags COLUMN_SCALAR --type ShortText
[[0,1294292406.01251,0.049535938],true]
> table_create --name IndexBlog1 --flags TABLE_PAT_KEY|KEY_NORMALIZE --key_type ShortText --default_tokenizer TokenBigram
[[0,1294292406.2629,0.040450231],true]
> column_create --table IndexBlog1 --name index_title --flags COLUMN_INDEX|WITH_POSITION --type Blog1 --source title
[[0,1294292406.50413,0.089260402],true]
> column_create --table IndexBlog1 --name index_message --flags COLUMN_INDEX|WITH_POSITION --type Blog1 --source message
[[0,1294292406.7942,0.116405104],true]
> load --table Blog1
> [
> {"_key":"grn1","title":"groonga test","message":"groonga message"},
> {"_key":"grn2","title":"baseball result","message":"rakutan eggs 4 - 4 groonga moritars"},
> {"_key":"grn3","title":"groonga message","message":"none"}
> ]
[[0,1294292407.11144,1.001433691],3]

match_columnsオプションで、検索対象のカラムを複数指定することが出来ます。検索する文字列はqueryオプションで指定します。これを使うことで、タイトルと本文を全文検索することができます。

実際に検索してみましょう。

実行例

> select --table Blog1 --match_columns title||message --query groonga
[[0,1294292408.31388,0.000823528],[[[3],[["_id","UInt32"],["_key","ShortText"],["title","ShortText"],["message","ShortText"]],[1,"grn1","groonga test","groonga message"],[3,"grn3","groonga message","none"],[2,"grn2","baseball result","rakutan eggs 4 - 4 groonga moritars"]]]]
> select --table Blog1 --match_columns title||message --query message
[[0,1294292408.5178,0.000566064],[[[2],[["_id","UInt32"],["_key","ShortText"],["title","ShortText"],["message","ShortText"]],[3,"grn3","groonga message","none"],[1,"grn1","groonga test","groonga message"]]]]
> select --table Blog1 --match_columns title --query message
[[0,1294292408.72074,0.0004798],[[[1],[["_id","UInt32"],["_key","ShortText"],["title","ShortText"],["message","ShortText"]],[3,"grn3","groonga message","none"]]]]

4.7.1.2. 複数のカラムにまたがったインデックスを付与する場合

内容は上の例とほぼ同じですが、titleとmessageの2つのカラムに対するインデックスが共通になっており、インデックスカラムが1つしかありません。

共通のインデックスを用いても、titleカラムのみでの検索、messageカラムのみでの検索、titleもしくはmessageカラムでの検索、全ての検索を行うことができます。

実行例

> table_create --name Blog2 --flags TABLE_HASH_KEY --key_type ShortText
[[0,1294292408.92333,0.107518903],true]
> column_create --table Blog2 --name title --flags COLUMN_SCALAR --type ShortText
[[0,1294292409.23168,0.050369538],true]
> column_create --table Blog2 --name message --flags COLUMN_SCALAR --type ShortText
[[0,1294292409.48288,0.049530167],true]
> table_create --name IndexBlog2 --flags TABLE_PAT_KEY|KEY_NORMALIZE --key_type ShortText --default_tokenizer TokenBigram
[[0,1294292409.73325,0.04885075],true]
> column_create --table IndexBlog2 --name index_blog --flags COLUMN_INDEX|WITH_POSITION|WITH_SECTION --type Blog2 --source title,message
[[0,1294292409.98288,0.105489516],true]
> load --table Blog2
> [
> {"_key":"grn1","title":"groonga test","message":"groonga message"},
> {"_key":"grn2","title":"baseball result","message":"rakutan eggs 4 - 4 groonga moritars"},
> {"_key":"grn3","title":"groonga message","message":"none"}
> ]
[[0,1294292410.28918,1.001414874],3]

実際に検索してみましょう。結果は上の例と同じになります。

実行例

> select --table Blog2 --match_columns title||message --query groonga
[[0,1294292411.49151,0.000575701],[[[3],[["_id","UInt32"],["_key","ShortText"],["title","ShortText"],["message","ShortText"]],[1,"grn1","groonga test","groonga message"],[2,"grn2","baseball result","rakutan eggs 4 - 4 groonga moritars"],[3,"grn3","groonga message","none"]]]]
> select --table Blog2 --match_columns title||message --query message
[[0,1294292411.69532,0.000523916],[[[2],[["_id","UInt32"],["_key","ShortText"],["title","ShortText"],["message","ShortText"]],[1,"grn1","groonga test","groonga message"],[3,"grn3","groonga message","none"]]]]
> select --table Blog2 --match_columns title --query message
[[0,1294292411.89825,0.000508446],[[[1],[["_id","UInt32"],["_key","ShortText"],["title","ShortText"],["message","ShortText"]],[3,"grn3","groonga message","none"]]]]

4.7.2. インデックス名を指定した全文検索

執筆中です。

4.7.3. インデックスの重み

執筆中です。