Search code examples
ruby-on-railselasticsearchtire

Rails tire:import with custom logic


I have a Ruby on rails 3.2 application where I'm trying to use Tire and Elastic Search.

I have a User model that has the following declarations:

include Tire::Model::Search
include Tire::Model::Callbacks

I then carried out an initial import of records into Elastic Search by calling:

rake environment tire:import CLASS=User FORCE=true

Is it possible to customise the import task, such that it skips one user? I have a system user that I would prefer not to be indexed?


Solution

  • First, the Rake task is only a convenience method for the most usual cases, when trying elasticsearch/Tire out, etc. For more complex situations, you should write your own indexing code -- it should be very easy.

    Second, if you have certain conditions whether the record is indexed or not, you should do what the README instructs you: don't include Tire::Model::Callbacks and manage the indexing lifecycle yourself, eg with:

    after_save do
      update_index if state == 'published'
    end