« May 2006 | Main | July 2006 »

2006.06.09

UTF-8での全文検索メモ(2)

前回の続きです。
Tsearch2 を使ったら、あっさりと Learning Path Finder でシラバスの全文検索ができるようになりました。
http://bavarois.db.tokushima-u.ac.jp/~miyoshi/lpf/ (←β版)

これを本運用サーバの LPF でも使えるようにしようと思い、どこに全文検索用 DB を置こうかと相談したところ、Mac OS X Server 上がいいんじゃない?ってことになりました。
本運用でも FreeBSD だろうという目論見が外れたため、Mac OS X での PostgreSQL + Tsearch2 + MeCab 環境の構築をすることに…。
その Mac OS X Server には fink で既にポスグレが入ってて、Tsearch2 も動いてるっぽい。ってことは、あとは MeCab と pgmecab を入れれば良いということです。
普段 fink を使っていないので、案の定、少しハマってしまったのでメモを残しておきます。

環境
・Mac OS X Server 10.4 (Xserve)
・PostgreSQL 7.4.8 インストール済み (Tsearch2 も動いてる)

方針
・できるだけ fink から
・UTF-8 が使えるように

1. 形態素解析エンジン MeCab (和布蕪) をインストール
 (Fink のヘルプページを参考に unstable なパッケージもインストールできるようにしておく。)
$ sudo fink install mecab
$ sudo vi /sw/fink/10.4-transitional/unstable/main/finkinfo/text/mecab-ipadic.info
 ConfigureParams: の行を書き換える
ConfigureParams: --prefix=%p --libexecdir=%p/lib --with-dicdir=%p/lib/mecab/dic/ipadic --with-mecab-config=%p/bin/mecab-config --with-charset=utf8
$ sudo fink install mecab-ipadic

2. mecab が動くかテストしてみる
(辞書ファイルの場所がおかしい場合は /sw/etc/mecabrc の dicdir を書き換える)

3. pgmecab をコンパイルするのに postgresql のソースが必要なので make しておく
$ cd /tmp
$ tar jxf /sw/src/postgresql-7.4.8.tar.bz2
$ cd postgresql-7.4.8
$ ./configure --prefix=/sw --docdir=/sw/share/doc --mandir=/sw/share/man --enable-multibyte --enable-recode --with-CXX --without-perl --without-python --without-openssl --with-libraries=/sw/lib --with-includes=/sw/include --without-tcl --without-tk --without-java --enable-odbc --with-pam --with-rendezvous --enable-syslog --with-krb5=/usr
(/sw/fink/10.4-transitional/stable/main/finkinfo/database/postgresql74-unified.info を参考に)
$ make

4. pgmecab をインストール
(pgmecab-1.1を展開しておく)
$ cd ~/Desktop/pgmecab-1.1
$ vi Makefile
 次の2行を書き換える
MECAB_CONFIG_PATH = /sw/bin/mecab-config
top_builddir = /tmp/postgresql-7.4.8
$ sudo make install

5. /sw/.../postgresql-7.4 等に入って欲しかったけど、/sw/.../postgresql に入ってしまったので、手作業で移動…orz。
$ cd /sw/share/postgresql/contrib
$ sudo mv pgmecab.sql ../../postgresql-7.4/contrib/
$ cd /sw/lib/postgresql
$ sudo mv pgmecab.so /sw/lib/postgresql-7.4/
$ cd /sw/share/doc/postgresql74-unified/contrib
$ sudo mv ../../postgresql/contrib/README.pgmecab .
(不要になったディレクトリを削除)
$ cd /sw/share
$ sudo rm -r postgresql
$ cd /sw/lib
$ sudo rm -r postgresql
$ cd /sw/share/doc
$ sudo rm -r postgresql

| | Comments (3) | TrackBack (0)

2006.06.06

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 (1) | TrackBack (2)

« May 2006 | Main | July 2006 »