acts_as_searchableのfulltext_searchの:findオプション
2007.07.05
acts_as_searchableを使っているのですが、「ぉ!」と思った検索オプションがあった(そして日本語ではまだ知られていないぽい?)ので紹介しておきます。また使い倒していないので、勘違いがあるかもしれませんが。
http://poocs.net/2006/4/6/introducing-acts-as-searchable
http://ar-searchable.rubyforge.org/
Article.fulltext_search("biscuits AND gravy")
Article.fulltext_search("biscuits AND gravy", :limit => 15, :offset => 14)
Article.fulltext_search("biscuits AND gravy", :attributes => "tag STRINC food")
Article.fulltext_search("biscuits AND gravy", :attributes => ["tag STRINC food", "@title STRBW Biscuit"])
Article.fulltext_search("biscuits AND gravy", :order => "@title STRA")
Article.fulltext_search("biscuits AND gravy", :raw_matches => true)
Article.fulltext_search("biscuits AND gravy", :find => { :order => :title, :include => :comments })
:findオプションや、:offsetもちゃんとあるんですね。
:findオプションはドキュメント内のソースを見る限り、Hyperestraiyer(HE)で検索してから、ヒットしたARのIDを元にARのfind検索をしていて、その際の:findオプション条件として渡せるようです。ただ:conditioinsを渡した場合、その条件での結果と、HEでマッチした結果が一致しない場合は、”ActiveRecord::RecordNotFound: Couldn't find all Items with IDs”というエラーが帰ってきます。:order、:includeぐらいにしておいた方がいいかもですね。
http://poocs.net/2006/4/6/introducing-acts-as-searchable
http://ar-searchable.rubyforge.org/
- limit - Maximum number of records to retrieve (default: 100)
- offset - Number of records to skip (default: 0)
- order - Hyper Estraier expression to sort the results (example: @title STRA, default: ordering by score)
- attributes - String to append to Hyper Estraier search query
- raw_matches - Returns raw Hyper Estraier documents instead of instantiated AR objects
- find - Options to pass on to the ActiveRecord::Base#find call
Article.fulltext_search("biscuits AND gravy")
Article.fulltext_search("biscuits AND gravy", :limit => 15, :offset => 14)
Article.fulltext_search("biscuits AND gravy", :attributes => "tag STRINC food")
Article.fulltext_search("biscuits AND gravy", :attributes => ["tag STRINC food", "@title STRBW Biscuit"])
Article.fulltext_search("biscuits AND gravy", :order => "@title STRA")
Article.fulltext_search("biscuits AND gravy", :raw_matches => true)
Article.fulltext_search("biscuits AND gravy", :find => { :order => :title, :include => :comments })
:findオプションや、:offsetもちゃんとあるんですね。
:findオプションはドキュメント内のソースを見る限り、Hyperestraiyer(HE)で検索してから、ヒットしたARのIDを元にARのfind検索をしていて、その際の:findオプション条件として渡せるようです。ただ:conditioinsを渡した場合、その条件での結果と、HEでマッチした結果が一致しない場合は、”ActiveRecord::RecordNotFound: Couldn't find all Items with IDs”というエラーが帰ってきます。:order、:includeぐらいにしておいた方がいいかもですね。
投稿者 : 日比 知子 | 投稿日時 : 2007.07.05 14:12
あすなろBLOGのトラックバック・コメントは承認制になっています。
すぐにブログに反映されませんので、ご了承ください。






名前:さる2007年07月23日 18:01
私もacts_as_searchableを使っていますが、いろいろ癖がありますのでご注意下さい。例として4点ほど挙げておきます。
(もし以下ご存知の箇所がありましたら、お教えてください。)
1.英語での部分検索
TommyをTomでは検索出来ません。(完全N-gram指定が必要だそうです。)
2.検索指定
例えば、ブログテーブルと記事テーブルがある(1toNの関係)として、指定したブログの記事だけを検索する方法がありません。
3.エラー
インデックスを張っていない範囲を検索するとエラーとなります。空白検索などは一撃です。
4.最大数の検索ルール
デフォルトでは検索がMAX100件となっていると思いますが、100件を選ぶルールがデフォルトでは不明です。(order等を指定するのが必須です。)