rs-pro / mongoid-elasticsearch

DEPRECATED - Consider using SearchKick https://github.com/ankane/searchkick
MIT License
61 stars 23 forks source link

rake es:reindex does not work, if models are not eager loaded #11

Open werbitzky opened 10 years ago

werbitzky commented 10 years ago

the comand rake es:reindex does not work for me (rails 4.1.4, mongoid 4.0.0rc2) the problem seems to be, that the models are not loaded before executing the task, so there are no registered models, i.e. Mongoid::Elasticsearch.registered_models is an empty array

  task :reindex => ['es:require'] do
    Mongoid::Elasticsearch.registered_models.each do |model_name|
      pb = nil
      model_name.constantize.es.index_all do |steps, step|
        pb = ProgressBar.create(title: model_name, total: steps, format: '%t: %p%% %a |%b>%i| %E') if pb.nil?
        pb.increment
      end
    end
  end
glebtv commented 10 years ago

It seems to be that the only way to do what you want is to manually try to Dir.glob all possible model directories, like rails_admin does https://github.com/sferik/rails_admin/blob/master/lib/rails_admin/config.rb#L307

I currently do not want to do that, as it's far from perfect in rails_admin.

Will leave this issue open for now.

This task should work with no problems in production, or if you manually require or require_dependency the models before it.

werbitzky commented 9 years ago

I agree that the Dir.glob solution is far from perfect

a quickfix for me was to manually add an initializer file that initializes the elasticsearch indexed models and thus makes the gem aware of them before executing the task

for example, if I had a "Product" and an "Offer" model I would just invoke their class constant in the initializer file like this

Offer
Product

thank you for this nice gem by the way!

dfabreguette commented 8 years ago

+1 It would be awesome to find a workaround !