|
ActsAsTsearchMethod
How to declare acts_as_tsearch in your class
IntroductionThis covers the basics of acts_as_tsearch declaration and not the find_by_tsearch method - which has a lot to it. Also see: Ground Rules
SimpleThis would search the description field of the blog_entries table: class BlogEntry < ActiveRecord::Base
acts_as_tsearch :fields => "description"
end
results = BlogEntry.find_by_tsearch("who what where")Multiple fieldsThis would search multiple fields in blog_entries table: class BlogEntry < ActiveRecord::Base
acts_as_tsearch :fields => ["title","description"]
end
results = BlogEntry.find_by_tsearch("who what where")Weighted searchThis is still experimental. Up to 4 weights are supported, weight's are currently ignored and hard coded to the values in the example below. This will search for fields in the users table, placing more importance on "a", then "b" and so on. Only four weights are support and only one is required. class User < ActiveRecord::Base
acts_as_tsearch :fields => {
"a" => {:columns => [:first_name, :last_name, :company_name], :weight => 1.0},
"b" => {:columns => [:short_description], :weight => 0.4},
"c" => {:columns => [:state_name, :county_name, :city_name], :weight => 0.2},
"d" => {:columns => [:about_me], :weight => 0.1}
}
end
results = User.find_by_tsearch("who what where")Multi Table SearchesThis is very experimental. I would love help on this from more experienced Rails programmers on how to clean it up Say you wanted to search blog_entries and their comments... BlogEntry.acts_as_tsearch :vectors => {
:fields => {
"a" => {:columns => ["blog_entries.title"], :weight => 1},
"b" => {:columns => ["blog_comments.comment"], :weight => 0.5}
},
:tables => {
:blog_comments => {
:from => "blog_entries b2
left outer join blog_comments on
blog_comments.blog_entry_id = b2.id",
:where => "b2.id = blog_entries.id"
}
}
}
results = BlogEntry.find_by_tsearch("who what where")As you can see this is pretty hackish. It's only been lightly tested with two tables so far, still a work in progress. Complete Method DefinitionTo Do: needs work Fully verbose call example Model.acts_as_tsearch :vectors => {
:locale => "default",
:auto_update_index => true,
:fields => {
"a" => {:columns => ["blog_entries.title"], :weight => 1},
"b" => {:columns => ["blog_entries.description"], :weight => 0.4},
"c" => {:columns => ["blog_entries.name"], :weight => 0.2},
"d" => {:columns => ["blog_comments.comment"], :weight => 0.1}
},
:tables => {
:blog_comments => {
:from => "blog_entries b2 left outer join
blog_comments on blog_comments.blog_entry_id = b2.id",
:where => "b2.id = blog_entries.id"
}
},
:another_vector => {... same format as above ...}
|
Model.acts_as_tsearch :vectors => { :local => "default", :auto_update_index => true,':local' should be ':locale'.
Thanks for the feedback. I updated the Wiki page.
hi to pevent errors with postgesql 8.3.x the tsearch line in the model has to be
class name_of_the_model < ActiveRecord::Base acts_as_tsearch :vectors => {:fields => ["a","b"], :locale => "public.name_of_the_tsearch_config"} endit took me several days to find out 8( i ony hope it helps the next guy 8)
Does anyone have a working example of a HABTM relationship?
I would like to do one like the multi table search example above, except with tags instead of comments.
Thanks! Brian
Well, I don't fully understand the SQL, but I was able to get it working with a many-to-many relationship
See: http://www.pastie.org/459372
class Page < ActiveRecord?::Base
endthis will index the tag names
As far as I can tell, the multi-table example is broken: it will only index one of the blog_comments. Investigation; suggestions welcome.
OK, this seems to get all the associated values when indexing a to-many relationship:
acts_as_tsearch :fields => ['title', 'body',
barmstrong, you want to do something like this with your code.