postgresの全文検索インデックス

全文検索用に GiST, GIN の2種類のインデックスが用意されている。

https://www.postgresql.jp/document/9.3/html/textsearch-indexes.html

GiST は tsvector またはtsquery 型のカラムに対して
GIN は tsvector型のカラムに対して
インデックスを付与できる。

スペース区切りの単語の文字列を元に tsvector 型のデータを生成できるらしい。
https://www.postgresql.jp/document/9.3/html/datatype-textsearch.html#DATATYPE-TSVECTOR

つまり、スペースで分かち書きされた文書を tsvector型のカラムに突っ込んでおけば、それに全文検索インデックスを付与できる。
textsearch_ja とか使わなくても、postgresの外で形態素解析し、分かち書きした結果を tsvector 型で入れておけば、そこに GiST/GIN インデックスを付与することができそう。

textsearch_ja は、どうやら to_tsvector 関数に日本語用の処理をプラグインし、日本語の場合は形態素解析分かち書きしたものを tsvector として登録するように拡張しているだけっぽい。
http://textsearch-ja.projects.pgfoundry.org/textsearch_ja.html#tsearch


なお、tsqueryは検索クエリで、単語を論理演算子でつないだ文字列を元に生成できるらしい。
https://www.postgresql.jp/document/9.3/html/datatype-textsearch.html#DATATYPE-TSQUERY

検索する際は、tsvector が tsquery に一致するかどうかを返す演算子"@@"を使う。
https://www.postgresql.jp/document/9.3/html/textsearch-tables.html#TEXTSEARCH-TABLES-SEARCH
https://www.postgresql.jp/document/9.3/html/functions-textsearch.html