Jens Krämer

Acts as ferret

 |  typo, ferret, ruby, rails

ferret Feeling the urgent need to try out David Balmain’s Ruby port of the famous Lucene search engine I integrated Ferret into this Typo installation.

“Nice, but this works exactly like the original MySQL based search.”

Well, nearly. Thanks to Typo, all Contents (Blog Articles, Comments to Articles and so called Pages) share a common base class, Content. And that’s where we hook in Ferret. So ferret not only finds articles matching your queries, but pages and articles where only a comment matches, too. Not to mention the mighty Ferret Query Language ;-)

The main part, the acts_as_ferret plugin, is based on those offered in the Ferret Wiki. I just cleaned them up a bit to fit my needs.

Want that, too ? Just put the plugin into vendor/plugins/acts_as_ferret/lib, and create a corresponding init.rb containing a simple require 'acts_as_ferret' in vendor/plugins/acts_as_ferret.

Now add the following to app/models/content.rb: acts_as_ferret :index_dir => "#{RAILS_ROOT}/index/contents", :fields => [ 'title', 'author', 'body', 'keywords' ,'extended', 'email', 'url', 'name' ]

class « self

# map comments to their articles, make articles unique and handle query parsing errors def ferret_find(query) find_by_contents( query ).map { |content| if content.is_a? Comment content.article else content end }.uniq rescue Exception => e [] end </code>

Next modify LiveController#search and ArticlesController#search to use the new search:

@results = Content.ferret_find(@search) unless @search.to_s.blank?

And you’re nearly done. All that is left is to modify the corresponding views app/views/live/search.rhtml and app/views/articles/search.rhtml to display correct links in case Page instances are found. Hint: page_link page.title, page



Any chance this can be made to use the mysql database, same as typo?


I don't think that storing the index in the database is possible. Why would you want to do that ?