Question

For some reason, on heroku it will not allow me to import models (this is for the Bonsai ElasticSearch add-on). It's just showing connection refused. I'm not sure how to resolve this. I've searched the internet and I can't find anything that will even hint me in the right direction to solve this. I checked to make sure the server was up and manually created the index with curl. Do the initializers get launched when running a rake task?

I set the elasticsearch url in the config/initializers/bonsai.rb file:

config/initializers/bonsai.rb

if Rails.env == 'production'
  ENV['ELASTICSEARCH_URL'] = ENV['BONSAI_URL']
end

Gemfile snippet

# Used for elastic search
gem 'elasticsearch-model', github: 'elasticsearch/elasticsearch-rails'
gem 'elasticsearch-rails', github: 'elasticsearch/elasticsearch-rails'
gem 'multi_json'
gem 'json'

Error

heroku run rake environment elasticsearch:import:all DIR=app/models
Running `rake environment elasticsearch:import:all DIR=app/models` attached to terminal... up, run.5751
[IMPORT] Loading models from: app/models
[IMPORT] Processing model: Firm...
rake aborted!
Connection refused - connect(2)
/app/vendor/bundle/ruby/2.0.0/gems/faraday-0.9.0/lib/faraday/adapter/net_http.rb:80:in `perform_request'
/app/vendor/bundle/ruby/2.0.0/gems/faraday-0.9.0/lib/faraday/adapter/net_http.rb:39:in `call'
/app/vendor/bundle/ruby/2.0.0/gems/faraday-0.9.0/lib/faraday/rack_builder.rb:139:in `build_response'
/app/vendor/bundle/ruby/2.0.0/gems/faraday-0.9.0/lib/faraday/connection.rb:377:in `run_request'
/app/vendor/bundle/ruby/2.0.0/gems/elasticsearch-transport-1.0.1/lib/elasticsearch/transport/transport/http/faraday.rb:21:in `block in perform_request'
/app/vendor/bundle/ruby/2.0.0/gems/elasticsearch-transport-1.0.1/lib/elasticsearch/transport/transport/base.rb:187:in `call'
/app/vendor/bundle/ruby/2.0.0/gems/elasticsearch-transport-1.0.1/lib/elasticsearch/transport/transport/base.rb:187:in `perform_request'
/app/vendor/bundle/ruby/2.0.0/gems/elasticsearch-transport-1.0.1/lib/elasticsearch/transport/transport/http/faraday.rb:20:in `perform_request'
/app/vendor/bundle/ruby/2.0.0/gems/elasticsearch-transport-1.0.1/lib/elasticsearch/transport/client.rb:102:in `perform_request'
/app/vendor/bundle/ruby/2.0.0/gems/elasticsearch-api-1.0.1/lib/elasticsearch/api/actions/bulk.rb:81:in `bulk'
/app/vendor/bundle/ruby/2.0.0/bundler/gems/elasticsearch-rails-26a3ba246968/elasticsearch-model/lib/elasticsearch/model/importing.rb:78:in `block in import'
/app/vendor/bundle/ruby/2.0.0/bundler/gems/elasticsearch-rails-26a3ba246968/elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb:88:in `block in __find_in_batches'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/relation/batches.rb:125:in `find_in_batches'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/querying.rb:9:in `find_in_batches'
/app/vendor/bundle/ruby/2.0.0/bundler/gems/elasticsearch-rails-26a3ba246968/elasticsearch-model/lib/elasticsearch/model/proxy.rb:80:in `method_missing'
/app/vendor/bundle/ruby/2.0.0/bundler/gems/elasticsearch-rails-26a3ba246968/elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb:86:in `__find_in_batches'
/app/vendor/bundle/ruby/2.0.0/bundler/gems/elasticsearch-rails-26a3ba246968/elasticsearch-model/lib/elasticsearch/model/importing.rb:77:in `import'
/app/vendor/bundle/ruby/2.0.0/bundler/gems/elasticsearch-rails-26a3ba246968/elasticsearch-model/lib/elasticsearch/model.rb:113:in `import'
/app/vendor/bundle/ruby/2.0.0/bundler/gems/elasticsearch-rails-26a3ba246968/elasticsearch-rails/lib/elasticsearch/rails/tasks/import.rb:59:in `block (3 levels) in <top (required)>'
/app/vendor/bundle/ruby/2.0.0/bundler/gems/elasticsearch-rails-26a3ba246968/elasticsearch-rails/lib/elasticsearch/rails/tasks/import.rb:99:in `block (4 levels) in <top (required)>'
/app/vendor/bundle/ruby/2.0.0/bundler/gems/elasticsearch-rails-26a3ba246968/elasticsearch-rails/lib/elasticsearch/rails/tasks/import.rb:82:in `each'
/app/vendor/bundle/ruby/2.0.0/bundler/gems/elasticsearch-rails-26a3ba246968/elasticsearch-rails/lib/elasticsearch/rails/tasks/import.rb:82:in `block (3 levels) in <top (required)>'
Tasks: TOP => elasticsearch:import:model
(See full trace by running task with --trace)

by the way can someone create an 'elasticsearch-rails' tag since its the upcoming replacement for Tire

Was it helpful?

Solution

Elasticsearch Rails does not use the same mechanism as Tire to set its URL in production on Heroku. To have this work correctly with Bonsai search, you have to have a different url specification. Fortunately a guy has made a gem (https://rubygems.org/gems/bonsai-elasticsearch-rails) to eliminate the needless, mandatory creation of a initializer by everyone.

Simply add this gem to your gem file under your production group. The version may be subject to change since it's so new:

gem 'bonsai-elasticsearch-rails', '~> 0.0.4'

If you're more curious about what this gem actually does, the entire source is below:

require "bonsai/elasticsearch/rails/version"

puts "Starting up a new ElasticSearch client with #{ENV['BONSAI_URL']}"
Elasticsearch::Model.client = Elasticsearch::Client.new url: ENV['BONSAI_URL']

I know, simple right...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top