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

4.6. タグ検索・参照関係の逆引き

本チュートリアルで、groongaはカラム値として他のテーブルへの参照の配列を持つことができることを紹介いたしました。実は、テーブルへの参照の配列データを用いることによって、いわゆるタグ検索を行うことが可能となります。

タグ検索はgroongaの転置インデックスというデータ構造を用いて高速に行われます。

4.6.1. タグ検索

動画共有サイトの検索エンジンを作ることを想定します。1つの動画には、その動画の特徴を表す、複数の語句が付与されています。「ある語句が付与されている動画の一覧を取得する」検索を行いたいとします。

実際に、動画情報のテーブルを作成し、検索をしてみましょう。

動画の情報を保存する、Videoテーブルを作成します。Videoテーブルでは、動画のタイトルをtitleカラムに、動画のタグ情報をtagsカラムにTagテーブル型で複数格納しています。 タグの情報を保存する、Tagテーブルを作成します。Tagテーブルでは、タグ文字列を主キーに格納し、Videoテーブルのtagsカラムに対するインデックスをindex_tagsカラムに格納しています。

実行例

> table_create --name Video --flags TABLE_HASH_KEY --key_type UInt32
[[-22,1308820912.80154,0.000455314,"already used name was assigned: <Video>",[["grn_obj_register","db.c",5938,"(stdin)",1274,"table_create --name Video --flags TABLE_HASH_KEY --key_type UInt32"]]],false]
> table_create --name Tag --flags TABLE_HASH_KEY --key_type ShortText
[[-22,1308820913.00517,0.00046206,"already used name was assigned: <Tag>",[["grn_obj_register","db.c",5938,"(stdin)",1275,"table_create --name Tag --flags TABLE_HASH_KEY --key_type ShortText"]]],false]
> column_create --table Video --name title --flags COLUMN_SCALAR --type ShortText
[[-22,1308820913.20848,0.000212757,"already used name was assigned: <Video.title>",[["grn_obj_register","db.c",5938,"(stdin)",1276,"column_create --table Video --name title --flags COLUMN_SCALAR --type ShortText"]]],false]
> column_create --table Video --name tags --flags COLUMN_VECTOR --type Tag
[[-22,1308820913.41095,0.000428695,"already used name was assigned: <Video.tags>",[["grn_obj_register","db.c",5938,"(stdin)",1277,"column_create --table Video --name tags --flags COLUMN_VECTOR --type Tag"]]],false]
> column_create --table Tag --name index_tags --flags COLUMN_INDEX --type Video --source tags
[[-22,1308820913.61454,0.00024036,"already used name was assigned: <Tag.index_tags>",[["grn_obj_register","db.c",5938,"(stdin)",1278,"column_create --table Tag --name index_tags --flags COLUMN_INDEX --type Video --source tags"]]],false]
> load --table Video
> [
> {"_key":1,"title":"Soccer 2010","tags":["Sports","Soccer"]},
> {"_key":2,"title":"Zenigata Kinjirou","tags":["Variety","Money"]},
> {"_key":3,"title":"groonga Demo","tags":["IT","Server","groonga"]},
> {"_key":4,"title":"Moero!! Ultra Baseball","tags":["Sports","Baseball"]},
> {"_key":5,"title":"Hex Gone!","tags":["Variety","Quiz"]},
> {"_key":6,"title":"Pikonyan 1","tags":["Animation","Pikonyan"]},
> {"_key":7,"title":"Draw 8 Month","tags":["Animation","Raccoon"]},
> {"_key":8,"title":"K.O.","tags":["Animation","Music"]}
> ]
[[0,1308820913.81791,2.009808857],8]

インデックスカラムを作成すると、全文検索が高速に行えるようになります。インデックスカラムは、対象のカラムに保存されたデータに更新があったとき、自動的に更新されます。

「ある語句が付与されている動画の一覧を取得する」検索を行いましょう。

実行例

> select --table Video --query tags:@Variety --output_columns _key,title
[[0,1308820916.02932,0.000242814],[[[2],[["_key","UInt32"],["title","ShortText"]],[2,"Zenigata Kinjirou"],[5,"Hex Gone!"]]]]
> select --table Video --query tags:@Sports --output_columns _key,title
[[0,1308820916.23053,0.000231493],[[[2],[["_key","UInt32"],["title","ShortText"]],[1,"Soccer 2010"],[4,"Moero!! Ultra Baseball"]]]]
> select --table Video --query tags:@Animation --output_columns _key,title
[[0,1308820916.43258,0.000233013],[[[3],[["_key","UInt32"],["title","ShortText"]],[6,"Pikonyan 1"],[7,"Draw 8 Month"],[8,"K.O."]]]]

このように、「Variety」、「Sports」、「Animation」のようなタグで検索を行うことができました。

4.6.2. 参照関係の逆引き

groongaはテーブル間の参照関係の逆引きを高速に行うためのインデックスを付与することができます。タグ検索は、その1例にすぎません。

例えば、ソーシャルネットワーキングサイトにおける友人関係を逆引き検索することができます。

以下の例では、ユーザー情報を格納するUserテーブルを作成し、ユーザー名を格納するusernameカラム、ユーザーの友人一覧を配列で格納するfriendsカラムとそのインデックスのindex_friendsカラムを追加しています。

実行例

> table_create --name User --flags TABLE_HASH_KEY --key_type ShortText
[[-22,1308820916.63478,0.000187163,"already used name was assigned: <User>",[["grn_obj_register","db.c",5938,"(stdin)",1293,"table_create --name User --flags TABLE_HASH_KEY --key_type ShortText"]]],false]
> column_create --table User --name username --flags COLUMN_SCALAR --type ShortText
[[-22,1308820916.8372,0.000179938,"already used name was assigned: <User.username>",[["grn_obj_register","db.c",5938,"(stdin)",1294,"column_create --table User --name username --flags COLUMN_SCALAR --type ShortText"]]],false]
> column_create --table User --name friends --flags COLUMN_VECTOR --type User
[[-22,1308820917.03936,0.000393764,"already used name was assigned: <User.friends>",[["grn_obj_register","db.c",5938,"(stdin)",1295,"column_create --table User --name friends --flags COLUMN_VECTOR --type User"]]],false]
> column_create --table User --name index_friends --flags COLUMN_INDEX --type User --source friends
[[-22,1308820917.24229,0.00020492,"already used name was assigned: <User.index_friends>",[["grn_obj_register","db.c",5938,"(stdin)",1296,"column_create --table User --name index_friends --flags COLUMN_INDEX --type User --source friends"]]],false]
> load --table User
> [
> {"_key":"ken","username":"健作","friends":["taro","jiro","tomo","moritapo"]}
> {"_key":"moritapo","username":"森田","friends":["ken","tomo"]}
> {"_key":"taro","username":"ぐるんが太郎","friends":["jiro","tomo"]}
> {"_key":"jiro","username":"ぐるんが次郎","friends":["taro","tomo"]}
> {"_key":"tomo","username":"トモちゃん","friends":["ken","hana"]}
> {"_key":"hana","username":"花子","friends":["ken","taro","jiro","moritapo","tomo"]}
> ]
[[0,1308820917.44495,1.609341731],6]

指定したユーザーを友人リストに入れているユーザーの一覧を表示してみましょう。

実行例

> select --table User --query friends:@tomo --output_columns _key,username
[[0,1308820919.25622,0.000551379],[[[5],[["_key","ShortText"],["username","ShortText"]],["ken","健作"],["taro","ぐるんが太郎"],["jiro","ぐるんが次郎"],["moritapo","森田"],["hana","花子"]]]]
> select --table User --query friends:@jiro --output_columns _key,username
[[0,1308820919.46056,0.000443467],[[[3],[["_key","ShortText"],["username","ShortText"]],["ken","健作"],["taro","ぐるんが太郎"],["hana","花子"]]]]

さらに、ドリルダウンを使って、友人リストに入っている数の一覧を表示してみましょう。

実行例

> select --table User --limit 0 --drilldown friends
[[0,1308820919.66284,0.000254632],[[[6],[["_id","UInt32"],["_key","ShortText"],["username","ShortText"],["index_friends","User"],["friends","User"]]],[[6],[["_key","ShortText"],["_nsubrecs","Int32"]],["taro",3],["jiro",3],["tomo",5],["moritapo",2],["ken",3],["hana",1]]]]

このように、テーブルの参照関係を逆にたどる検索ができました。

4.6.3. インデックス付きジオサーチ

位置情報のカラムに対して、インデックスを付与することが出来ます。大量の位置情報レコードを検索する場合に、検索速度が速くなります。

実行例

> table_create --name GeoIndex --flags TABLE_PAT_KEY --key_type WGS84GeoPoint
[[0,1308820919.86871,0.074996815],true]
> column_create --table GeoIndex --name index_point --type Site --flags COLUMN_INDEX --source location
[[0,1308820920.14512,0.125822381],true]
> load --table Site
> [
>  {"_key":"http://example.org/","location":"128452975x503157902"},
>  {"_key":"http://example.net/","location":"128487316x502920929"}
> ]
[[0,1308820920.47239,0.80439359],2]
> select --table Site --filter 'geo_in_circle(location, "128515259x503187188", 5000)' --output_columns _key,location
[[0,1308820921.4776,0.000305925],[[[1],[["_key","ShortText"],["location","WGS84GeoPoint"]],["http://example.org/","128452975x503157902"]]]]

同様に、位置情報レコードを用いてソートする場合に、ソート速度が速くなります。

実行例

> select --table Site --filter 'geo_in_circle(location, "128515259x503187188", 50000)' --output_columns _key,location,_score --sortby '-geo_distance(location, "128515259x503187188")' --scorer '_score = geo_distance(location, "128515259x503187188")'
[[0,1308820921.67976,0.00036066],[[[2],[["_key","ShortText"],["location","WGS84GeoPoint"],["_score","Int32"]],["http://example.org/","128452975x503157902",2054],["http://example.net/","128487316x502920929",6720]]]]