生产中的 Searchkick + ElasticSearch:Faraday::ConnectionFailed:连接被拒绝 - 连接(2)
Searchkick + ElasticSearch in Production: Faraday::ConnectionFailed: Connection refused - connect(2)
我有一个使用 ElasticSearch 的生产应用程序。 (有效)但现在我可以添加 searchkick gem 而不是仅使用 elasticsearch。
为此我添加了以下内容:
在我的Gemfile中添加:
gem 'elasticsearch-model'
gem 'elasticsearch-rails'
gem 'searchkick'
在我的 config/initializers/elasticsearch.rb 中(我正在使用 Amazon Elasticsearch Service 进行生产。好的,我知道这是不好的做法,但我会为环境变量改变这个)
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
显示下一条消息:Faraday::ConnectionFailed:连接被拒绝 - connect(2)
之后我试试这个
rake searchkick:reindex:all RAILS_ENV=production
我也收到了同样的信息。有什么想法吗?
写完这篇文章后post我看了这篇文章:
in 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} })
我希望这个解释对其他人有用
我找到上面的答案让我完成了 95% 的事情。我听从了 Nelson 的建议并创建了一个 Elasticsearch 初始化器,它消除了 Faraday::ConnectionFailed (Connection refused - connect(2) for "localhost" port 9200):
但是我仍然需要重新索引并迁移我的 Heroku 数据库。
heroku run rake searchkick:reindex CLASS=YOUR CLASS NAME
然后heroku run rake db:migrate
希望这能为任何像我这样卡在最后一部分的 Heroku 新手澄清。
如果您使用的是 gem 'searchkick'
,只需在 /config/initializers/elasticsearch.rb
中添加以下行
ENV["ELASTICSEARCH_URL"] = "http://myelasticsearch-xyz-foobar.amazonaws.com:port"
我有一个使用 ElasticSearch 的生产应用程序。 (有效)但现在我可以添加 searchkick gem 而不是仅使用 elasticsearch。
为此我添加了以下内容:
在我的Gemfile中添加:
gem 'elasticsearch-model'
gem 'elasticsearch-rails'
gem 'searchkick'
在我的 config/initializers/elasticsearch.rb 中(我正在使用 Amazon Elasticsearch Service 进行生产。好的,我知道这是不好的做法,但我会为环境变量改变这个)
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
显示下一条消息:Faraday::ConnectionFailed:连接被拒绝 - connect(2)
之后我试试这个
rake searchkick:reindex:all RAILS_ENV=production
我也收到了同样的信息。有什么想法吗?
写完这篇文章后post我看了这篇文章:
in 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} })
我希望这个解释对其他人有用
我找到上面的答案让我完成了 95% 的事情。我听从了 Nelson 的建议并创建了一个 Elasticsearch 初始化器,它消除了 Faraday::ConnectionFailed (Connection refused - connect(2) for "localhost" port 9200):
但是我仍然需要重新索引并迁移我的 Heroku 数据库。
heroku run rake searchkick:reindex CLASS=YOUR CLASS NAME
然后heroku run rake db:migrate
希望这能为任何像我这样卡在最后一部分的 Heroku 新手澄清。
如果您使用的是 gem 'searchkick'
,只需在 /config/initializers/elasticsearch.rb
ENV["ELASTICSEARCH_URL"] = "http://myelasticsearch-xyz-foobar.amazonaws.com:port"