Tanker - IndexTank integration to your favorite orm

Note: This gem is still quite alpha but please feel free to send comments

IndexTank is a great search indexing service, this gem tries to make any orm keep in sync with indextank with ease. This gem has a simple but powerful approach to integrating IndexTank to any orm. The gem also supports pagination using the will_paginate/collection.

There are very few things needed to integrate Tanker to any orm. Basically the orm instance must respond to id and any attributes you wish to index.

Installation

gem install tanker

If you are using Rails 3 its better to add Tanker to your GEMFILE

gem 'tanker'

And run

bundle install

Initialization

If you’re using Rails, config/initializers/tanker.rb is a good place for this:

YourAppName::Application.config.index_tank_url = 'http://:[email protected]'

If you are not using rails you can put this somewhere before you load your modesl

Tanker.configuration = {:url => 'http://:[email protected]' }

You would probably want to have a fancier configuration depending on your environment. Be sure to copy and paste the correct url provided in the IndexTank Dashboard

Example

class Topic < ActiveRecord::Base
  acts_as_taggable

  # just include the Tanker module
  include Tanker

  # define the index by supplying the index name and the fields to index
  # this is the index name you create in the Index Tank dashboard
  # you can use the same index for various models Tanker can handle
  # indexing searching on different models with a single Index Tank index
  tankit 'my_index' do
    indexes :title
    indexes :content
    indexes :tag_list #NOTICE this is an array of Tags! Awesome!
  end

  # define the callbacks to update or delete the index
  # these methods can be called whenever or wherever
  # this varies between ORMs
  after_save :update_tank_indexes
  after_destroy :delete_tank_indexes

  # Note! Will Paginate pagination, thanks mislav!
  def self.per_page
    5
  end

end

Create Topics

Topic.create(:title => 'Awesome Topic', :content => 'blah, blah', :tag_list => 'tag1, tag2')
Topic.create(:title => 'Bad Topic', :content => 'blah, blah', :tag_list => 'tag1')
Topic.create(:title => 'Cool Topic', :content => 'blah, blah', :tag_list => 'tag3, tag2')

Search Topics

@topics = Topic.search_tank('Topic', :page => 1, :per_page => 10) # Gets 3 results!
@topics = Topic.search_tank('blah',  :conditions => {:tag_list => 'tag1'}) # Gets 2 results!
@topics = Topic.search_tank('blah',  :conditions => {:title => 'Awesome', :tag_list => 'tag1'}) # Gets 1 result!

Paginate Results

<% will_paginate(@topics) %>

Reindex your data

If you are using rails 3 there are of couple of rake tasks included in your project

rake tanker:clear_indexes

This task deletes all your indexes and recreates empty indexes, if you have not created the indexes in the Index Tank interface this task creates them for you provided you have enough indexes in your account.

rake tanker:reindex

This task re-indexes all the your models that have the Tanker module included. Usually you will only need a single Index Tank index but if you want to separate your indexes please use different index names in the tankit method call in your models

TODO

  • Documentation

  • More fancy stuff (Suggestions?)

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Acknowledgements

The gem is based on a couple of projects:

*redis-textsearch *mongomapper *thinkingtank *will_paginate

Kudos to this awesome projects and developers

Copyright © 2010 @kidpollo. See LICENSE for details.