Nelson Patricio Jimenez Nelson Patricio Jimenez - 5 months ago 212
Ruby Question

Searchkick + ElasticSearch in Production: Faraday::ConnectionFailed: Connection refused - connect(2)

I have an application in production with ElasticSearch. (It works) but now I can add searchkick gem instead use only elasticsearch.

For this I added the following:

In my Gemfile added:

gem 'elasticsearch-model'
gem 'elasticsearch-rails'
gem 'searchkick'


In my config/initializers/elasticsearch.rb (I'm using Amazon Elasticsearch Service for production. Ok I know this is bad practices but I'll go change this for Environment variables)

if Rails.env == "production"
Elasticsearch::Model.client = Elasticsearch::Client.new url: 'https://xxxxxxxx.xxxxxxx.amazonaws.com/'

else
Elasticsearch::Model.client = Elasticsearch::Client.new url: 'http://localhost:9200/'
end


in models/product.rb

require 'elasticsearch/model'

class Product < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
searchkick
#currently use this function for search. (It works)
scope :searching, ->(query) { __elasticsearch__.search(query).records }
end


but now when I try to use in the server:

Product.search("foobar") #before run: rails c production


Show me the next message: Faraday::ConnectionFailed: Connection refused - connect(2)

After that I try this

rake searchkick:reindex:all RAILS_ENV=production


And I got the same message. Any idea for this ?

Answer

After I finished write this post I read this: Searchkick with ElasticSearch returns "Faraday::ConnectionFailed: execution expired"

in config/initializers/elasticsearch.rb

if Rails.env == "production"
  url = 'https://search-shipit-search-6l6jwr5uhyg5evytk6o3unidya.us-east-1.es.amazonaws.com/'
    Elasticsearch::Model.client = Elasticsearch::Client.new url: url
  Searchkick.client = Elasticsearch::Client.new(hosts: url, retry_on_failure: true, transport_options: {request: {timeout: 250}})
else
  url = 'http://localhost:9200/'
    Elasticsearch::Model.client = Elasticsearch::Client.new url: url
  Searchkick.client = Elasticsearch::Client.new(hosts: url, retry_on_failure: true, transport_options: {request: {timeout: 250}})
end

After refactoring and clean this code I got this:

  Elasticsearch::Model.client = Elasticsearch::Client.new url: env['RAILS_ENV']
  Searchkick.client = Elasticsearch::Client.new(hosts: env['RAILS_ENV'], retry_on_failure: true, transport_options: {request: {timeout: 250} })

I hope this explanation works for others

Comments