简体   繁体   中英

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?

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

I've found a rough solution to my problem and wanted to post something back, just in case someone else comes across this. If anyone has any better suggestions, please let me know.

In the end I wrote a tire task that calls the regular import all and then subsequently deletes the system account from the index.

namespace :tire do
  desc 'Create search index on User'
  task :index_users => :environment do
    ENV['CLASS'] = 'User'
    ENV['FORCE'] = 'TRUE'
    Rake::Task['tire:import'].invoke
    @user = User.find_by_type('System')
    User.tire.index.remove @user
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM