Ludiaを使ってみる

PostgreSQL 8.1.4 起動と自動起動設定らへんを参考にPostgreSQLを起動した上で,LudiaのREADMEにあるサンプルを試してみた.

su - postgres
createdb test
psql -f /usr/local/src/ludia-0.8.0/pgsenna2.sql test
psql test

psql testPostgreSQLのターミナルに入るので,ここでテーブルを作ったり,データを入れたりしてみる.

create table table1 (col1 text, col2 varchar(128));
INSERT INTO table1 VALUES ('すもももももももものうち', 'あの壺はよいものだ');
INSERT INTO table1 VALUES ('ももから生まれた桃太郎', 'あの壷はよいものだ');
CREATE INDEX index1 ON table1 USING fulltext(col1);
CREATE INDEX index2 ON table1 USING fulltextb((col2::text));

で,続いて検索してみる.

select * from table1 where col1 @@ 'もも';
           col1           |        col2
--------------------------+--------------------
 すもももももももものうち | あの壺はよいものだ
 ももから生まれた桃太郎   | あの壷はよいものだ
(2 rows)

select * from table1 where col2 @@ '壷';
          col1          |        col2
------------------------+--------------------
 ももから生まれた桃太郎 | あの壷はよいものだ
(1 row)

SELECT col1, pgs2getscore(table1.ctid, 'index1') FROM table1 WHERE col1 @@ 'もも';
           col1           | pgs2getscore
--------------------------+--------------
 すもももももももものうち |           10
 ももから生まれた桃太郎   |            5
(2 rows)

1番目と2番目の方法は全文検索.検索スコアも取得する場合は,最後の方法のようにpgs2getscoreを利用する.

おしまい