Hatena::Grouphsj

HSJ.(中略).jp - いろいろお勉強コーナー このページをアンテナに追加 RSSフィード

2006年04月07日

[][]acts_as_searchableで全文検索してみる。 acts_as_searchableで全文検索してみる。 - HSJ.(中略).jp - いろいろお勉強コーナー を含むブックマーク

Rails blogに掲載されていたPlug into HyperEstraier with acts_as_searchableを試してみることにしました。

HyperEstraierのインストール

この全文検索プラグインHyper EstraierP2P機能を利用していますので、同ソフトインストールを先にする必要があります。

上記サイトのドキュメントに則してインストールします。

ぼくは面倒なのでWindows版のバイナリパッケージを使いました(^^;

配布パッケージを展開し、パスの通ったところに*.bat,*.exe,*.dllコピーします。

acts_as_searchableのインストール

まずはgemでインストール。サンプルは権限甘々Win系でやってるのですが、当然sudoなどを要する環境では頭につけてください。

C:\Rails>gem install acts_as_searchable
Attempting local installation of 'acts_as_searchable'
Local gem file not found: acts_as_searchable*.gem
Attempting remote installation of 'acts_as_searchable'
Updating Gem source index for: http://gems.rubyforge.org
Successfully installed acts_as_searchable-0.1.0
Installing RDoc documentation for acts_as_searchable-0.1.0...

適当アプリをこしらえてみます

scaffoldを使って適当CRUDを仕上げます。

C:\Rails>rails search --database sqlite3

C:\Rails>cd search

C:\Rails\search>ruby script\generate migration article
      create  db/migrate
      create  db/migrate/001_article.rb

(db/migrate/001_article.rbを編集)

C:\Rails\search>rake migrate
(in C:/Rails/search)
== Article: migrating =========================================================
-- create_table(:articles)
   -> 0.2410s
== Article: migrated (0.2410s) ================================================

C:\Rails\search>ruby script\generate scaffold article

db/migrate/001_article.rb
class Article < ActiveRecord::Migration
   def self.up
      create_table :articles do |t|
         t.column :subject, :string
         t.column :body, :text
         t.column :created_on, :time
      end
   end

   def self.down
      drop_table :articles
   end
end

とりあえず適当な記事をいくつか作っておきましょう(笑)

あ、とりあえず言っておきます。

environment.rbやapplication.rbに $KCODE='u' とかのお馴染みのやつをやっておきました。

全文検索P2Pの用意

全文検索導入する準備ができたので、環境を構築します。

概念的な理解はとりあえず放棄し(ぉい)、下記手順で作っちゃいましょう。

C:\>cd Estraier
C:\Estraier>estmaster init rails_acts_as_searchable
2006-04-07T01:41:31Z    INFO    the root directory created
2006-04-07T01:41:31Z    INFO    starting the user manager
2006-04-07T01:41:31Z    INFO    finishing the user manager
2006-04-07T01:41:31Z    INFO    saving the user list
2006-04-07T01:41:31Z    INFO    starting the node manager
2006-04-07T01:41:31Z    INFO    finishing the node manager
2006-04-07T01:41:31Z    INFO    synchronizing the node manager

これでC:\Estraierにrails_acts_as_searchableというフォルダが作成され、ここが全文検索ノードマスタ(と呼ばれるもの)になります。

次にノードマスタを起動します。

C:\Estraier>estmaster start rails_acts_as_searchable
2006-04-07T01:41:42Z    INFO    starting the master process (pid:1088)
2006-04-07T01:41:42Z    INFO    starting the user manager
2006-04-07T01:41:42Z    INFO    starting the node manager
2006-04-07T01:41:42Z    INFO    loading the user list
2006-04-07T01:41:42Z    INFO    waiting for requests

これが動いている間は全文検索ができる…わけです。終了するときはestmaster stop rails_acts_as_searchableとするか、Ctrl+CでOK。


続いて管理用インタフェイスhttp://localhost:1978/master_ui」に接続します。

標準認証が表示されますがユーザ名「admin」パスワード「admin」でログインできます。

ログイン後「Manage Nodes」を選択し、画面の下にある二つのテキストボックスに「development」「acts_as_searchableテスト用」と入力し、createボタンクリック


これで、Estraier側の設定は終わりです。


config/database.ymlの設定変更

次に結構意外なんですが、database.ymlの設定変更が必要となります。

とりあえず変更後のものをあげておきますと、以下のとおりで、estraier:というレベルが新しく追加されています。

# SQLite version 3.x
#   gem install sqlite3-ruby
development:
  adapter: sqlite3
  database: db/development.sqlite3
  estraier:
    host: localhost
    user: admin
    password: admin
    port: 1978
    node: development

# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
test:
  adapter: sqlite3
  database: db/test.sqlite3

production:
  adapter: sqlite3
  database: db/production.sqlite3

ここで設定されているuser/password/nodeについては先ほどのノードマスタの設定内容に依存しますので、個別変更が必要なポイントとなります。

modelにacts_as_searchableする

class Article < ActiveRecord::Base
   acts_as_searchable
end
Article.reindex!

acts_as_searchableのRDocを見れば分かりますが、標準ではbodyというフィールドだけがインデックスされます。

全文検索の対象を広げたい場合は↓みたいな感じにすればいいみたいですが、試してません(^^;

class Article < ActiveRecord::Base
   acts_as_searchable :searchable_fields => [:subject, :body]
end
Article.reindex!
念のため

config/environment.rbに

require_gem 'acts_as_searchable'

を追加しておいた。


あとはViewとControllerに適用するだけ!

scaffoldで作成されたlist.rhtmlにお粗末な検索フォームを追加します。

<%= start_form_tag :action => 'fulltext_search' %>
	<%= text_field 'search', 'keyword'  %>
	<%= submit_tag "検索" %>
<%= end_form_tag %>

そして、articles_controller.rbにfulltext_searchアクションを追加します。

def search
   @articles = Article.fulltext_search(params[:search][:keyword])
   render :action => 'list'
end

おお、ちゃんと動いてる!!

普通に日本語も通ってます(当たり前だ)

ちなみに

前掲のblog記事のコメント欄を見ているとApache Luceneをpure RubyポーティングしたFerretというプロジェクトがあるそうで、しかもacts_as_ferretなんかもあるそうです(笑)


まぁ、今回のEstraierとacts_as_searchableとの関係と同じですね。

そのうち気が向いたら、取り上げたいと思います(ぉい

farmarmarfarmarmar2006/12/02 02:34Wazzzup. It is a nice site!
<a href=http://phentermine.alldating.org/phentermine.htm>phentermine</a>
Its'not a spam, help sick children! I am very sorry!

jihbbhadjihbbhad2006/12/02 11:11Wazzzup. Very interesting, really!
cheap <a href=http://phentermine.alldating.org/phentermine.htm>phentermine</a>
Don't delete. Help homeless children, thanks

AVExpertsAVExperts2006/12/02 23:47Hi,
I'd like to present you the <a href=http://anti-virus.free-alta.info/anti-best-software-virus.htm>best anti virus software</a>.
Would you like to protect your system? Secure it now!

Thanks,
Anti Virus Experts

kvarterakvartera2006/12/03 09:07Halo. You site is realy cool!
<a href=http://viagra.alldating.org/viagra.htm>viagra</a>
Its'not a spam, help sick children! I am very sorry!

msonyxxmsonyxx2006/12/06 09:10Halo. It is a nice site!
<a href=http://viagra.alldating.org/viagra.htm>viagra</a>
<a href="http://viagra.alldating.org/viagra.htm">viagra</a>
Thanks

hromsgromhromsgrom2006/12/06 13:04Hello Help homeless children!
online [url=http://phentermine.alldating.org/phentermine.htm]phentermine[/url] online
<a href="http://phentermine.alldating.org/phentermine.htm">phentermine</a>
Bye

msonyxxmsonyxx2006/12/08 11:27Wazzzup. It is a nice site!
<a href=http://viagra.alldating.org/viagra.htm>viagra</a>
<a href="http://viagra.alldating.org/viagra.htm">viagra</a>
Thanks

mspherammspheram2006/12/09 06:28Hi. Cool!
Visit my site <a href=http://delta-space.info/phentermine.htm>viagra</a>
buy http://delta-space.info/phentermine.htm viagra online
Thanks.

mspherammspheram2006/12/09 09:41Good evening. Good work!
Visit my site <a href=http://delta-space.info/phentermine.htm>viagra</a>
buy http://delta-space.info/phentermine.htm viagra online
Thanks.

kubiksskubikss2006/12/09 14:09Hello. Cool!
Visit my site <a href=http://freeunixhs.info/phentermine.htm>phentermine</a>
buy http://freeunixhs.info/phentermine.htm phentermine online
Thanks.

kubiksskubikss2006/12/09 18:12Hi. Cool!
Visit my site <a href=http://freeunixhs.info/phentermine.htm>phentermine</a>
buy http://freeunixhs.info/phentermine.htm phentermine online
Thanks.

mortakrjakmortakrjak2006/12/10 01:03Hello Super site!
buy <a href=http://super-viagra.blogspot.com>viagra</a>
cheap <a href="http://super-viagra.blogspot.com">viagra</a> online
Bye

mortakrjakmortakrjak2006/12/10 04:50Hello Cool site!
buy <a href=http://super-viagra.blogspot.com>viagra</a>
cheap <a href="http://super-viagra.blogspot.com">viagra</a> online
Bye

dasfanviadasfanvia2006/12/10 22:28Very-very interesting!
buy <a href=viagra.alldating.org/viagra.htm>viagra</a>
cheap <a href="viagra.alldating.org/viagra.htm">viagra</a> online
Bye

dasfanviadasfanvia2006/12/11 02:17Ja das ist fantastish!
buy <a href=viagra.alldating.org/viagra.htm>viagra</a>
cheap <a href="viagra.alldating.org/viagra.htm">viagra</a> online
Bye

chrviaggichrviaggi2006/12/11 10:22Hello!
buy <a href=http://viagra.alldating.org/viagra.htm>viagra</a>,
cheap http://viagra.alldating.org/viagra.htm online
G'night

chrviaggichrviaggi2006/12/11 14:04Very interesting!
buy <a href=http://viagra.alldating.org/viagra.htm>viagra</a>,
cheap http://viagra.alldating.org/viagra.htm online
Bye

xrjuviabexrjuviabe2006/12/14 09:16Hi all, try our products!
buy <a href=http://xrjuhosting.info/phentermine.htm>phentermine</a> online,
cheap http://xrjuhosting.info/phentermine.htm phentermine
Sorry.

voldarkunvoldarkun2006/12/15 07:15Hi, nice site.
visit <a href=http://stop-smoking-aid.batcave.net/smoking-stop.htm>smoking stop</a>
http://stop-smoking-aid.batcave.net/smoking-stop.htm smoking stop
Thanks.

voldarkunvoldarkun2006/12/15 12:54Hi, interesting site.
visit <a href=http://stop-smoking-aid.batcave.net/smoking-stop.htm>smoking stop</a>
http://stop-smoking-aid.batcave.net/smoking-stop.htm smoking stop
Thanks.

reisereisereisereise2006/12/18 11:15Hi, very intersting cite. Thanks for information!
[url=http://models4.azure-shift.info/tawny-roberts.htm]tawny roberts[/url]
<a href="http://models4.azure-shift.info/tawny-roberts.htm">tawny-roberts</a>
G'night

dcmorphedcmorphe2006/12/22 11:05Hi, cool site. Look at my site.
buy [url=http://www.rhodesschool.com/blogcomments/default.asp?blogID=23193]phentermine[/url]
http://www.rhodesschool.com/blogcomments/default.asp?blogID=23193 phentermine
G'night

dcmorphedcmorphe2006/12/23 04:16Hi all!, good design. Look at my site.
cheap [url=http://www.rhodesschool.com/blogcomments/default.asp?blogID=23193]phentermine[/url]
http://www.rhodesschool.com/blogcomments/default.asp?blogID=23193 phentermine
G'night

missviasolmissviasol2006/12/24 05:47Good morning, cool site.
my home page [url=http://viagra-store.info/]viara[/url].
Buy http://viagra-store.info#viagra best.
G'night.

missviasolmissviasol2006/12/24 11:49Good morning, interesting.
my home page [url=http://viagra-store.info/]viara[/url].
Buy http://viagra-store.info#viagra best.
thanks a lot.

mrdrugbukmrdrugbuk2006/12/24 22:36Wazzup, thanks author.
my home page [url=http://www.youthpeer.org/forum/topic.asp?TOPIC_ID=327]viara[/url].
Buy http://www.youthpeer.org/forum/topic.asp?TOPIC_ID=327#viagra cheap.
Bye-bye.

mrdargetmrdarget2006/12/25 04:39Hi, cool site.
visit me at [url=http://www.theologicalforum.org/forum/topic.asp?TOPIC_ID=58]phentermine[/url].
Buy http://www.theologicalforum.org/forum/topic.asp?TOPIC_ID=58#phentermine cheap.
thanks.

lifeislifelifeislife2006/12/25 13:56 Hallo, interesting.
See [url=http://www.jahk.org/forum/topic.asp?TOPIC_ID=113]viagra[/url].
Test http://www.jahk.org/forum/topic.asp?TOPIC_ID=113#viagra best.
Bye-bye.

uskimrouskimro2006/12/26 02:07Hi all, fantastic design.
Go here [url=http://bowtrol-herbal.info]viagra[/url].
Test http://bowtrol-herbal.info#viagra cheap.
thanks.

ramaramaramarama2006/12/26 10:42Halo, nice design.
And I am here [url=http://courses.forum.ncsu.edu/cgi-bin/netforum/exp_netforum/a/14--59.1.1]viagra[/url].
Take http://courses.forum.ncsu.edu/cgi-bin/netforum/exp_netforum/a/14--59.1.1#viagra best.
thanks a lot.

pupsichekkpupsichekk2006/12/31 03:47Nice day, fantastic design.
Look at my site [url=http://tuoppi.oulu.fi/kbs-bin/readbeer?Nr=626]viagra[/url].
Test http://tuoppi.oulu.fi/kbs-bin/readbeer?Nr=626#viagra online.
thanks.

pupsishepupsishe2007/01/01 11:41Hey, cool design.
Go here [url=http://tuoppi.oulu.fi/kbs-bin/readbeer?Nr=626]viagra[/url].
Buy http://tuoppi.oulu.fi/kbs-bin/readbeer?Nr=626#viagra online.
Bye.

puppikkpuppikk2007/01/02 07:11Good morning, fantastic design.
Look at [url=http://tuoppi.oulu.fi/kbs-bin/readbeer?Nr=626]viagra[/url].
Take http://tuoppi.oulu.fi/kbs-bin/readbeer?Nr=626#viagra online.
Bye-bye.

tuppikkktuppikkk2007/01/02 17:52Good morning, nice design.
And I am here [url=http://tuoppi.oulu.fi/kbs-bin/readbeer?Nr=626]viagra[/url].
Test http://tuoppi.oulu.fi/kbs-bin/readbeer?Nr=626#viagra best.
Bye.

hljupikkkhljupikkk2007/01/03 07:00Hey, interesting information.
Look at my site [url=http://tuoppi.oulu.fi/kbs-bin/readbeer?Nr=626]viagra[/url].
See http://tuoppi.oulu.fi/kbs-bin/readbeer?Nr=626#viagra online.
Bye.

warrdevawarrdeva2007/01/04 19:12Good morning, nice design.
My home page [url=http://www.jahk.org/forum/topic.asp?TOPIC_ID=113]viagra[/url].
Take http://www.jahk.org/forum/topic.asp?TOPIC_ID=113#viagra best.
Bye-bye.

craps game craps game 2007/01/19 09:44Find Choose [link=http://www.wizzcom.com.au/templates/web/craps-3.html] online craps [/link] http://www.wizzcom.com.au/templates/web/craps-3.html <a href=http://www.wizzcom.com.au/templates/web/craps-3.html>craps</a> [url=http://www.wizzcom.com.au/templates/web/craps-3.html]play craps online free[/url].

casino en linea casino en linea 2007/01/20 03:21<a href=http://casinoenlinea.mi-direccion.com/>http://casinoenlinea.mi-direccion.com/, casino en linea </a>http://casinoenlinea.mi-direccion.com/

play poker play poker 2007/01/26 05:02
I wish you the best of luck the site is really great and if there is anything my organization can do to help you make it even more informative please let me know.
http://loyno.edu/~klmyers/casino/poker-3.html | http://loyno.edu/~klmyers/casino/slots-4.html | http://www.asbestosandyou.com.au/catalog/work/new/keno-1.html | http://loyno.edu/~sonier/online/casino-2.html |

free online craps free online craps 2007/01/26 05:12
I just wanted you to know how much I appreciate your support.
http://loyno.edu/~sonier/gamble/blackjack-2.html | http://www.asbestosandyou.com.au/catalog/work/new/keno-4.html | http://www.asbestosandyou.com.au/catalog/images/cache/craps-3.html | http://www.asbestosandyou.com.au/catalog/work/new/texas-hold-em-3.html |

VarvabsoVarvabso2007/02/12 14:47Hi all, interesting!
http://m-5.uml.edu/_disc2/00000021.htm?effexor
[url=http://m-5.uml.edu/_disc2/00000021.htm?effexor]effexor[/url]
<a href="http://m-5.uml.edu/_disc2/00000021.htm?effexor">effexor</a>
Regards

bivimortabivimorta2007/02/12 22:17Hi all, thanks author!
http://m-5.uml.edu/_disc2/00000022.htm?paxil
[url=http://m-5.uml.edu/_disc2/00000022.htm?paxil]paxil[/url]
<a href="http://m-5.uml.edu/_disc2/00000022.htm?paxil">paxil</a>
Thanks

gopapopagopapopa2007/02/13 02:48Hi, i found a lot of new information here!
http://m-5.uml.edu/_disc2/00000023.htm?cialis
[url=http://m-5.uml.edu/_disc2/00000023.htm?cialis]cialis[/url]
<a href="http://m-5.uml.edu/_disc2/00000023.htm?cialis">cialis</a>
G'night

gogenrogengogenrogen2007/02/13 06:29Wazzzzup, super site!
http://m-5.uml.edu/_disc2/00000024.htm?viagra
[url=http://m-5.uml.edu/_disc2/00000024.htm?viagra]viagra[/url]
<a href="http://m-5.uml.edu/_disc2/00000024.htm?viagra">viagra</a>
Best wishes

hvartenghvarteng2007/02/13 09:25Hallo all, cool!
http://m-5.uml.edu/_disc2/00000025.htm?soma
[url=http://m-5.uml.edu/_disc2/00000025.htm?soma]soma[/url]
<a href="http://m-5.uml.edu/_disc2/00000025.htm?soma">soma</a>
Regards

mintenddamintendda2007/02/13 16:52Halo, cool desing!
http://m-5.uml.edu/_disc2/00000026.htm?prozac
[url=http://m-5.uml.edu/_disc2/00000026.htm?prozac]prozac[/url]
<a href="http://m-5.uml.edu/_disc2/00000026.htm?prozac">prozac</a>
Best Regards

supersigasupersiga2007/02/13 20:19Hello, cool!
http://m-5.uml.edu/_disc2/00000027.htm?zoloft
[url=http://m-5.uml.edu/_disc2/00000027.htm?zoloft]zoloft[/url]
<a href="http://m-5.uml.edu/_disc2/00000027.htm?zoloft">zoloft</a>
Best wishes

spektrumerspektrumer2007/02/14 02:15Halo, nice site!
http://m-5.uml.edu/_disc2/00000028.htm?lipitor
[url=http://m-5.uml.edu/_disc2/00000028.htm?lipitor]lipitor[/url]
<a href="http://m-5.uml.edu/_disc2/00000028.htm?lipitor">lipitor</a>
Thanks

ViagraViagra2007/02/24 09:54You have made a good site
<a href="http://kuso.cc/1e6P">viagra</a>
http://kuso.cc/1e6P
[url=http://kuso.cc/1e6P]cheap viagra[/url]

wager wager 2007/03/20 20:17
It's very exciting!!

<a href=http://medlem.spray.se/casinolibre/gamble-1.html>wager</a> foul
<a href=http://web.hpt.jp/onlinecasino/roulette-1.html>online roulette poker</a> over fifth
<a href=http://casino-online1.freecities.com/casino-descargas-2.html>casinos descargas internet</a> bank
<a href=http://www.aldeamix.com/argentina/fcngrtones/free-ringtones-2.html>free ringtones for sprint phone</a> rank small

Lon KostLon Kost2007/03/30 07:59Usefull information and all is good arranged. Just want to say that I find your site enough interesting for me.

Zhopa2htmlZhopa2html2007/04/10 01:19Hi, nice site! <a href="http://xasfasfasfsafsa.com/pizdaeb-asa.html">asdaPizda</a>