UbuntuにHyper Estraierをいれてacts_as_searchableを使う

acts_as_searchableはmodelを全文検索するときに使うプラグインでHyper Estraierを使って検索する

Hyper Estraier

インストール
sudo apt-get install hyperestraier
/etc/default/hyperestraierを編集
NO_START=0

NO_STARTを0に変更。ブート時自動起動するようになる

起動
sudo /etc/init.d/hyperestraier start
管理画面

Manage NodesメニューからNode Createを選択しノードを作成

acts_as_searchable

インストール
ruby script/plugin install git://github.com/scoop/acts_as_searchable.git
database.yml

以下を追記する

:estraier
  :host
  :user
  :password
  :port
  :node

nodeに作成したノード名を設定

Modelの設定
acts_as_searchable

デフォルト:bodyが検索される

検索したいカラムの追加
acts_as_searchable :searchable_fields => [:hoge, :foo]
インデックスに値を格納する
act_as_searchable :attributes => [:hoge, :foo]
インデックスの再構築
ruby script/console
Model.reindex!
Controller側
Model.fulltext_search("word", option = {})
options
  • limit
  • offset
  • order
  • attributes
  • raw_matches
  • find