So, recently one of the object listings on one of my Rails sites
crossed the dreaded 50+ mark and It was time to add searching.
However, I didn’t want to just scan all the fields of a model or make
complicated queries, so I set out to write something more meaningful.
How do you use it? Like this.
First of all you add a column:
ALTER TABLE posts ADD COLUMN searchable varchar(250);
and then you tell something to your ActiveRecord
class Post < ActiveRecord::Base
indexes_columns :title, :tags, :body, :author
It will give you reasonably sane keywords for the columns. If the
columns are associations (which is the idea of the plugin also) they
will get indexed together with the main model. Of course your index
can get out of sync when you delete associated objects, but the thing
was meant to be simple, so don’t be too harsh.
Also, you can do:
class Post < ActiveRecord::Base
indexes_columns :title, :tags, :body, :author, :into=>:search_terms
which will index separate keywords into the table called
“search_terms” and give you a model to work with this table
After that you can query for stuff:
Post.find_with_term("Windows sucks") # I hope everyone is offended
which will issue a LIKE query for “windows” AND “sucks” if you use a
column in the model OR “windows” and “sucks” from the “search_terms”
table if you are using it instead. The “terms table” is clunky but
effective (I’ve seen it used many times in other projects so I
thought - why not).
All the index maintenance is performed automagically (or at least is
The only thing missing for complete joy is the third indexer, for
Ferret. Ferret is much more advanced and much more versatile than all
this rough crap, so what I would like to have is a thin wrapper
around it which “hooks” your ActiveRecords into ferret but still lets
you fine-tune everything. All through the same syntax.
The reason why I didn’t START with Ferret in the first place was
because I wanted to install it ASAP and TxD hasn’t got ferret on it’s
shareds yet. This was also not my original intention - I just needed
an elegant way for my clients to search their lists
The approach (in it’s current form) seems quite extensible (you can
make new Indexers yourself) and anyone who wants to tackle Ferret
integration (there are starting points) is free to do so - I will
have to learn the Ferret API first and I ain’t got time now.
If someone wants he can also implement fulltext searching for MySQL
etc. but I didn’t want to do it right away because I like the plugin
The plugin has some little dirty schticks to it that I implemented
and I consider nice,. for example dates will be indexed as “day of
week - month”, and if you search for “thursday” you should, among
others, get the blog entries written on, well, Thursdays. Which is
sorta nice. Absolutely not academic but nice.
So, the docs are here: http://www.julik.nl/code/active-search/
and the plugin itself can be pulled from my subversion
Any contributions are welcome. Note that the SVN URL is expected to
be unavailable sometimes because it’s on the server with a very bad
track record and I currently can’t do anything about it.
It was called Julik::SimpleSearch but I just bit the bullet and