今回も全文検索の具体例を見てみました。
やりたい事
PDFの本文を全文検索して、キーワードが含まれるパスを取得する
全体の流れ
- PDFをテキストに変換
- Groongaに投入
- この状態で検索
- インデックス作成
- 再度検索
やってみる
###1)PDFをテキストに変換
https://poppler.freedesktop.org/
などを使ってPDFをテキストに変換する
###2)Groongaに投入
テーブルとカラムを作成する
$ groonga -n ./pdfs.db < create-table.grn
create-table.grn
table_create pdfs \
TABLE_HASH_KEY \
ShortText
column_create pdfs body \
COLUMN_SCALAR \
Text
データを投入(データはJSONにする)
$ groonga pdfs.db < load.grn
load.grn
https://gist.github.com/kou/6baea90d8acc714745fb#file-load-grn
###3)この状態で検索
0.02秒で検索できました。
$ groonga pdfs.db < select.grn
[
[
0,
1458908822.06077,
0.0255997180938721
],
[
[
[
1
],
[
[
"_key",
"ShortText"
]
],
[
"/usr/share/doc/virtualbox/UserManual.pdf"
]
]
]
]
select.grn
select pdfs \
--query body:@API \
--output_columns _key \
--output_pretty yes
###4)インデックス作成
$ groonga pdfs.db < create-index.grn
[[0,1458909124.8641,0.00386977195739746],true]
[[0,1458909124.86806,0.0781242847442627],true]
create-index.grn
# 「terms」以外はテンプレ
table_create terms \
TABLE_PAT_KEY \
ShortText \
--default_tokenizer TokenBigram \
--normalizer NormalizerAuto
# 「terms」と「body_index」と
# 「pdfs」と「body」以外はテンプレ
column_create terms body_index \
COLUMN_INDEX|WITH_POSITION \
pdfs \
body
###5)再度検索
0.0013秒で検索できました!
$ groonga pdfs.db < select.grn
[
[
0,
1458909188.45909,
0.00138068199157715
],
[
[
[
1
],
[
[
"_key",
"ShortText"
]
],
[
"/usr/share/doc/virtualbox/UserManual.pdf"
]
]
]
]
select.grn
select pdfs \
--query body:@API \
--output_columns _key \
--output_pretty yes
まとめ
インデックスを使うとやはり速い!
その他メモ
- PDFを検索するならHonyomiを使うのが良いかも
https://github.com/ongaeshi/honyomi
Ruby + Groonga で作られた検索エンジン
検索したPDFの画像が表示されるものでとても良くできているなと思いました!