UTF-8での全文検索メモ(PostgreSQL + Tsearch2 + MeCab 編)
慣れない perl に苦戦しているわけですが、"Bad free() ignored (PERL_CORE)" って警告が出てくる原因がイマイチわかりません…。
perl5.8を入れ直してみようかとportsでコンパイルするとエラーが出るようになるし…。困ったなぁ…。
で、それとは関係なくポスグレのTsearch2 + MeCabを使った全文検索の仕方を忘れないようにメモメモ。
参考にしたのは以下のページ。
https://www.oss.ecl.ntt.co.jp/tsearch2j/index.html
http://www.emaki.minidns.net/Programming/postgres/index.html
---
環境
・FreeBSD 5.4
・PostgreSQL 7.4.13 インストール済み
(/usr/ports/databases/postgresql74-server/)
方針
・できるだけ ports から
・UTF-8が使えるように (中国人の名前とかも扱うので…)
1. 形態素解析エンジン MeCab (和布蕪) をインストール
# cd /usr/ports/japanese/mecab
# make install clean
2. MaCab の辞書を UTF-8 でインストール
# cd /usr/ports/japanese/mecab-ipadic
Makefile.local ファイルを作成し,以下の行を追加
CONFIGURE_ARGS += --with-charset=utf8
# make install clean
3. Tsearch2 をインストール
# cd /usr/ports/databases/postgresql74-server
# make ← work ディレクトリを作るため
# cd work/postgresql-7.4.13/contrib/tsearch2
# gmake install clean ← make だとエラーが...
4. pgmecab をインストール
pgmecab-1.1 を展開しておく
Makefile の以下の3行を修正
MECAB_CONFIG_PATH = /usr/local/bin/mecab-config
top_builddir = /usr/ports/databases/postgresql74-server/work/postgresql-7.4.13
SHLIB_LINK = `$(MECAB_CONFIG_PATH) --libs` -lthr
# gmake install clean ← make だとエラーが...
5. sampledb データベースを作成
% createdb -E utf8 sampledb
6. sampledb に分かち書き関数を登録
% psql -e -f /usr/local/share/postgresql/contrib/pgmecab.sql sampledb
7.sampledb に Tsearch2 を登録
% psql -e -f /usr/local/share/postgresql/contrib/tsearch2.sql sampledb
8. psql で sampledb を開く
% psql sampledb
9. sampledb にテーブルを作成
sampledb=# create table tblSample (intIndex int4, strText text, idxFTI tsvector);
10. トリガの定義
sampledb=# create trigger tsvectorupdate before update or insert on tblSample for each row execute procedure tsearch2(idxFTI, pgmecab, strText);
11. インデックス用カラムに GiST インデックスを定義
sampledb=# create index idxFTI_idx on tblSample using gist(idxFTI);
12. 検索用のユーザ定義型と関数を定義
sampledb=# create type finddoctype as (findindex integer, findtext text);
sampledb=# create function finddoc(text) returns setof finddoctype as ' select findtbl.intIndex as findindex, findtbl.strText as findtext from ( select fromtbl.intIndex, fromtbl.strText from tblSample as fromtbl, to_tsquery(''simple'', replace(pgmecab($1), '' '', ''&'')) as q where fromtbl.idxfti @@ q offset 0 ) as findtbl where findtbl.strText like ''%'' || $1 || ''%''; ' language sql;
登録
sampledb=# insert into tblSample values (1, '登録テスト');
sampledb=# select * from tblsample ;
intindex | strtext | idxfti
----------+------------+---------------------
1 | 登録テスト | '登録':1 'テスト':2
(1 row)
更新
sampledb=# update tblSample set strText = '更新テスト' where intIndex = 1;
sampledb=# select * from tblsample ;
intindex | strtext | idxfti
----------+------------+---------------------
1 | 更新テスト | '更新':1 'テスト':2
(1 row)
検索
sampledb=# select * from finddoc('テスト');
findindex | findtext
-----------+------------
1 | 更新テスト
(1 row)
削除
sampledb=# delete from tblSample where intIndex = 1;





Comments
きのうYasSoの、行に関係したよ♪
でYasSoはここへエラーしなかったー。
Posted by: BlogPetのPlotter | 2006.06.09 at 13:27