ThinkingTank, a Ruby Gem

ThinkingTank is a simple ActiveRecord extension that allows you to define how to index your models and gives you easy tools to search them.

Setting up ThinkingTank

First you'll need to install the ThinkingTank ruby gem

gem install thinkingtank

Then in your rails app you'll have to require this gem by editing your Gemfile:

gem "thinkingtank"

You'll also need to add a config/indextank.yml file to configure your API url, for instance:

development:
    api_url: '<YOUR API URL HERE>'
    index_name: '<YOUR DEVELOPMENT INDEX NAME HERE>'

Indexing your models

To make your models indexable, you'll need to add a define_index block to your model class. For instance, if you have a Post model that defines a title and a body and you want both fields to be indexed you could do it like this:

class Post < ActiveRecord::Base
  define_index do
    indexes title
    indexes body
  end
end

This will allow you to use the search method in Post, like this:

Post.search(query)

Behind the scenes, ThinkingTank will submit a post for indexing after you've saved it. It will index each indexable field respecting the field names and it will create two special fields. One called "__any" with the union of every indexable field (this will be used by default when searching) and another one called "__type" which will have information to discriminate different models inside an index. You'll be able to search across all fields by using the default field but you can also ask for terms in specific fields by using the field:term syntax.

Once you have configured all the model you wanted you can use the rake task reindex to index all the existing that. This task will re-create the index (if it had never been created, this creates it for you), losing any data indexed in that index that is not in your DB and index every instance of the indexable models.

rake indextank:reindex

If you change the way your models are indexed you can also use that task to apply your changes retroactively