Searchkick + ElasticSearch in Production:Faraday :: ConnectionFailed:连接被拒绝 - 连接(2)

时间:2016-06-04 17:33:09

标签: ruby ruby-on-rails-4 elasticsearch amazon-ec2 searchkick

我在ElasticSearch的生产中有一个应用程序。 (它有效)但现在我可以添加searchkick gem而不是仅使用elasticsearch。

为此我添加了以下内容:

在我的 Gemfile 中添加:

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

在我的 config / initializers / elasticsearch.rb 中(我使用Amazon Elasticsearch Service进行制作。好的,我知道这是不好的做法,但我和#39 ; ll将其更改为环境变量)

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

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

但是现在当我尝试在服务器中使用时:

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

显示下一条消息:法拉第:: ConnectionFailed:拒绝连接 - 连接(2)

之后我试试这个

rake searchkick:reindex:all RAILS_ENV=production

我得到了同样的信息。对此有何想法?

3 个答案:

答案 0 :(得分:3)

写完这篇文章后,我读了这篇文章:Searchkick with ElasticSearch returns "Faraday::ConnectionFailed: execution expired"

config / initializers / elasticsearch.rb

中的

if Rails.env == "production"
  url = 'http://myelasticsearch-xyz-foobar.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

重构并清理此代码后,我得到了这个:

  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} })

我希望这个解释适用于其他人

答案 1 :(得分:1)

我发现上面的答案让我95%的方式。我遵循了Nelson的建议并创建了一个Elasticsearch初始化器,它消除了Faraday::ConnectionFailed (Connection refused - connect(2) for "localhost" port 9200): 但是我仍然需要重新索引和迁移我的Heroku DB。

heroku run rake searchkick:reindex CLASS=YOUR CLASS NAME

然后heroku run rake db:migrate

希望澄清任何像我一样的Heroku新手,他们被困在最后一部分。

答案 2 :(得分:0)

如果您使用的是gem 'searchkick',只需在/config/initializers/elasticsearch.rb中添加以下行

ENV["ELASTICSEARCH_URL"] = "http://myelasticsearch-xyz-foobar.amazonaws.com:port"