Wednesday, June 16, 2010

Ruby on Rails: Acts_as_xapian: Search engine (plugin)

Installing Xapian

The first step to installing Xapian is to download the latest version of xapian-core and extract:

curl -O
tar -xzvf xapian-core-1.0.10.tar.gz

From here, we need to configure and build the Xapian library:

cd xapian-core-1.0.10
sudo make install

Once this is complete, we need to download the Xapian bindings, which will let Xapian interact with Ruby, PHP and other scripting languages:

curl -O
tar -xzvf xapian-bindings-1.0.10.tar.gz

Again, now that we have downloaded them, we need to configure and build the Xapian-bindings:

cd xapian-bindings-1.0.10
sudo make install

Once that finishes, Xapian will be installed on your system. Of course, these instructions are for installing from source. If you are using a system that has aptitude installed, or another package manager, you can also install Xapian that way. For example, to install Xapian using aptitude:

aptitude search xapian

You need the following two files to complete the install:

libxapian15 - search engine library
libxapian-ruby1.8 - xapian search engine inferface for Ruby 1.8

Finally, simply enter the following commands to install the necessary components:

sudo aptitude install libxapian15
sudo aptitude install libxapian-ruby1.8


sudo apt-get install libxapian15 libxapian-ruby1.8

For more detailed instructions, visit

Installing the acts_as_xapian Rails plugin

script/plugin install git:// vendor/plugins/acts_as_xapian

Once the plugin is installed, run the migrations to add the required tables to your database:

script/generate acts_as_xapian
rake db:migrate

Setting Up The Models Code

To include fields to your index, you will need to add an “acts_as_xapian” call to the model you want to index.

class Lesson < ActiveRecord::Base
acts_as_xapian :texts => [:name, :description]

In this case, only the data found in the “name” and “description” attributes of our Lesson model will be searched.

Building and Updating the Xapian Indexes

Xapian indexing takes place “offline,” which means that the index is not automatically updated after a model/database is changed. While this may change in the future, currently the index must be built and updated periodically by you, or using a cron job (See Crontab Reference).

At this point, you can quickly test that your Xapian install, plugin, and model code are all working together via the following rake commands:

To build the index:
rake xapian:rebuild_index models="Post"

** do cron job for rebuild_index for automatically updated after a model/database is changed
** if you have more than 1 model to build the index, rake xapian:rebuild_index models="ModelName1 ModelName2"
eg: rake xapian:rebuild_index models="Post Lesson"

To update index:
rake xapian:update_index

To test index:
rake xapian:query models="Post" query="golf"

In controller.rb

query = 'software'

Basic Search:
search =[Post], query, :limit => 15)
@posts = search.results.collect {|p| p[:model]}

Similar Results:
similar =[Post], @posts, :limit => 15)
@similar_posts = similar.results.collect {|p| p[:model]}

In views.html.erb

<% for post in @posts %>
<% end %>

Related Link:
Pagination -

No comments:

Post a Comment