groonga

全文検索エンジンgroongaインストールメモ。

本家
http://groonga.org

groongaの特徴

全文検索ライブラリSennaの後継
・HTTPなどの複数プロトコルに対応
・高速なデータ更新
・複数プロセス・複数スレッドで共有できるストレージ
 MySQLプロトコルでデータの更新を行い、HTTPでデータの参照を行うことができます
・ドリルダウンなどの集計系クエリを高速に実現
Senna転置インデックス実装をさらに改良
・位置情報(緯度・経度)検索
・自動クエリキャッシュ機構

インストール

wget http://groonga.org/files/groonga/groonga-1.2.0.tar.gz

tar xvfz groonga-1.2.0.tar.gz

cd groonga-1.2.0

# 未指定の場合、/usr/local/bin、/usr/local/etcにインストールされる
./configure --prefix=/usr --localstatedir=/var && make && sudo make install

makeで数分待たされる

チュートリアル

↓をなぞっただけ・・・。
チュートリアル>基本的な操作

DB操作

・データベースを新規に作成
groonga -n ストレージファイルパス名

groonga -n /tmp/test01.db
> ctrl-d

ls /tmp
test01.db
test01.db.0000000

・状態表示
groonga DBパス名 [コマンド]

groonga /tmp/test01.db
>status
[[0,1301387542.74839,7.9e-05],{"alloc_count":160,"starttime":1301387115,"uptime":427,"version":"1.2.0","n_queries":1,"cache_hit_rate":0.0,"command_version":1,"default_command_version":1,"max_command_version":2}]


・テーブル作成
table_create

# ShortText型の主キー値を持ち、主キーの格納方法はHASHである、'test_table_01'という名前のテーブルを作成
> table_create --name test_table_01 --flags TABLE_HASH_KEY --key_type ShortText
[[0,1301387612.4288,0.014293],true]

# table表示
> table_list
[[0,1301387621.78855,0.00023],[[["id","UInt32"],["name","ShortText"],["path","ShortText"],["flags","ShortText"],["domain","ShortText"],["range","ShortText"]],[256,"test_table_01","/tmp/test01.db.0000100","TABLE_HASH_KEY|PERSISTENT","ShortText","null"]]]


・カラム作成

# 'test_table_01'テーブルにShortText型の通常のカラム値を持ち、'title'という名前のカラムを作成
>column_create --table test_table_01 --name title --flags COLUMN_SCALAR --type ShortText
[[0,1301387638.44455,0.014711],true]
# レコード確認
>select --table test_table_01
[[0,1301387648.57257,0.000223],[[[0],[["_id","UInt32"],["_key","ShortText"],["title","ShortText"]]]]]


・データロード

# データロード
> load --table test_table_01
> [
> {"_key":"http://example.org/","title":"This is test record 1!"},
> {"_key":"http://example.net/","title":"test record 2."},
> ]
[[0,1301387915.69299,26.312139],2]
# レコード確認
> select --table test_table_01
[[0,1301387970.58104,0.00015],[[[2],[["_id","UInt32"],["_key","ShortText"],["title","ShortText"]],[1,"http://example.org/","This is test record 1!"],[2,"http://example.net/","test record 2."]]]]


・検索

> select --table test_table_01 --query _id:1
[[0,1301388073.76527,0.000606],[[[1],[["_id","UInt32"],["_key","ShortText"],["title","ShortText"]],[1,"http://example.org/","This is test record 1!"]]]]
> select --table test_table_01 --query "title:\"test record 2.\""
[[0,1301388354.85375,0.000369],[[[1],[["_id","UInt32"],["_key","ShortText"],["title","ShortText"]],[2,"http://example.net/","test record 2."]]]]
>  select --table test_table_01 --query "_key:\"http://example.org/\""
[[0,1301388368.18982,8.8e-05],[[[1],[["_id","UInt32"],["_key","ShortText"],["title","ShortText"]],[1,"http://example.org/","This is test record 1!"]]]]
全文検索

↑からの続き

全文検索インデックス

全文検索用の語彙表の作成

# ShortText型の主キー値を持つ、’Terms’という名前のテーブルを作成
>table_create --name test_table_02 --flags TABLE_PAT_KEY|KEY_NORMALIZE --key_type ShortText --default_tokenizer TokenBigram
[[0,1301390119.32021,0.002017],true]

全文検索用のインデックスカラムの作成

# 語彙表にインデックス型のカラムを作成
> column_create --table test_table_02 --name blog_title --flags COLUMN_INDEX|WITH_POSITION --type test_table_01 --source title
[[0,1301390163.78398,0.021836],true]


全文検索

インデックスを用いた全文検索を行うこともできます

> select --table test_table_01 --query "title:@\"this\""
> select --table test_table_01 --match_columns title --query "record test nine"
command 内容
status groongaプロセスの状態を表示します。
table_list DBに定義されているテーブルのリストを表示します。
column_list テーブルに定義されているカラムのリストを表示します。
table_create DBにテーブルを追加します。
column_create テーブルにカラムを追加します。
select テーブルに含まれるレコードを検索して表示します。
load テーブルにレコードを挿入します。