# config/initializers/elasticsearch.rb
# ensure you set AWS_ES_URL, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY
# change region as needed
require 'patron'
require 'faraday_middleware/aws_signers_v4'
# arguably this should be defined elsewhere
class AmazonElasticSearchClient
def self.client
return Elasticsearch::Client.new(url: ENV['AWS_ES_URL']) do |f|
f.request :aws_signers_v4,
credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY']),
service_name: 'es',
region: 'us-west-2'
end
end
end
# for local development I run a local elasticsearch,
# so I only override the Searchkick.client in production environments
Searchkick.client = AmazonElasticSearchClient.client if Rails.env.production?