karmi / retire

A rich Ruby API and DSL for the Elasticsearch search engine
http://karmi.github.com/retire/
MIT License
1.86k stars 533 forks source link

import:all tasks: models load #982

Closed khcr closed 9 years ago

khcr commented 9 years ago

My models didn't load properly when I ran rake environment tire:import:all because I autoload models folders. I wrote in my application.rb: config.paths.add "app/models", eager_load: true, glob: "*" to load the subfolder models. After I got errors, I rewrote the import task like this:

namespace :tire do
    namespace :import do 
        task all: :environment do
            params = eval(ENV['PARAMS'].to_s) || {}
            Rails.application.eager_load!
            ActiveRecord::Base.descendants.each do |klass|

                # Skip if the class doesn't have Tire integration
                next unless klass.respond_to?(:tire)

                total  = klass.count rescue nil

                Tire::Tasks::Import.add_pagination_to_klass(klass)
                Tire::Tasks::Import.progress_bar(klass, total) if total

                index = klass.tire.index
                Tire::Tasks::Import.delete_index(index) if ENV['FORCE']
                Tire::Tasks::Import.create_index(index, klass)

               Tire::Tasks::Import.import_model(index, klass, params)
           end
           puts '[Import] Done.'
       end
   end
end

Maybe it can help. Thanks for the awesome work !

karmi commented 9 years ago

Thanks for the posting the code!

In the new gem, we do it a bit differently: https://github.com/elasticsearch/elasticsearch-rails/blob/master/elasticsearch-rails/lib/elasticsearch/rails/tasks/import.rb#L87, not sure if the trick is still needed, but it has worked fine so far.