ElasticSearch 5.0 + Rails - ruby-on-rails

Installed a fresh ElasticSearch 5.0 today and changed my Rails configuration to point to ES 5.
My elasticsearch.rb configuration file looks like:
require "faraday"
require "typhoeus/adapters/faraday"
config = {
host: "http://xxx.xxx.xxx.yyyy:9200/",
transport_options: {
request: { timeout: 5 }
},
}
if File.exists?("config/elasticsearch.yml")
config.merge!(YAML.load_file("config/elasticsearch.yml").symbolize_keys)
end
I have the following related gems installed on the application:
gem 'elasticsearch-model'
gem 'elasticsearch-rails'
gem 'elasticsearch-persistence', require: 'elasticsearch/persistence/model'
When I go to start my application, I receive the message:
[400] No handler found for uri [//****] and method [DELETE] (Elasticsearch::Transport::Transport::Errors::BadRequest)
Has anyone encountered this issue before?
I looked around for a bit and it looks like ElasticSearch 5.0 has a new API for deleting, but I'm not sure if this is the root cause of my issues:
https://www.elastic.co/guide/en/elasticsearch/reference/5.0/docs-delete-by-query.html
Thanks in advance!

According this discussion The problem is the proxy_options configuration. Just ignore the transport_options. If you change the configuration as following it should work.
config = {
hosts: default_host,
adapter: :typhoeus
}

Related

ElasticSearch maxClauseCount is set to 1024

I'm using the elasticsearch-rails gem in Rails 5 and I am trying to increase the max_clause_count. I'm using version 7.3 of ElasticSearch.
In my elasticsearch.yml file I have added the following code:
indices.query.bool.max_clause_count: 4096
In my initializer, I load the configs like so:
config = {
host: "http://localhost:9200",
transport_options: {
request: { timeout: 200 }
}
}
if File.exists?("config/elasticsearch.yml")
config.merge!(YAML.load_file("config/elasticsearch.yml").symbolize_keys)
end
Elasticsearch::Model.client = Elasticsearch::Client.new(config)
I can still reproduce this error with one of my queries that is rather long and when I do, after loading the configs above I still get the error:
{"type":"too_many_clauses","reason":"maxClauseCount is set to 1024"}
printing the config to the terminal I get this output:
{:host=>"http://localhost:9200", :transport_options=>{:request=>{:timeout=>200}}, :"indices.query.bool.max_clause_count"=>4096}
I hate to ask this but just in case this is the issue, this is a static Lucene setting, it can only be set in the Elasticsearch config file and get picked up at startup. Did you restart your cluster after making the config change?

Why is elasticserach-rails suddenly raising Faraday::ConnectionFailed (execution expired)?

I'm using Elasticsearch in a Rails app via the elasticsearch-model and elasticsearch-rails gems.
Everything was previously working fine, but after some updates I am now getting a Connection Failed error whenever I attempt to interact with the remote cluster (AWS Elasticsearch).
> MyModel.__elasticsearch__.create_index! force: true
=> Faraday::ConnectionFailed (execution expired)
I'm struggling to work out what is causing this connection error. After searching for similar issues, I've adjusted timeouts and tried various combinations of http, https and naked urls, but no success.
What is a sensible way to debug this connection error?
My Elasticsearch is initialized like this.
#initializers/elasticsearch.rb
require 'faraday_middleware'
require 'faraday_middleware/aws_sigv4'
credentials = Aws::Credentials.new(
ENV.fetch('AWS_ACCESS_KEY_ID'),
ENV.fetch('AWS_SECRET_ACCESS_KEY')
)
config = {
url: ENV.fetch('AWS_ELASTICSEARCH_URL'),
retry_on_failure: true,
transport_options: {
request: { timeout: 10 }
}
}
client = Elasticsearch::Client.new( config ) do |f|
f.request :aws_sigv4, credentials: credentials, service: 'es', region: ENV.fetch('AWS_ELASTICSEARCH_REGION')
end
Elasticsearch::Model.client = client
It turns out that there were two parts to this issue.
First, the Elasticsearch::Client, as configured above, was using the default ES port 9200. My ES is hosted on AWS, which appears to not expose this port.
After fixing this, I ran into the second issue (which I suspect is more specific to this app). I started getting a Faraday::ConnectionFailed (end of file) error. I don't know what caused this, but configuring the client with host and scheme fixed it.
My final config is as follows:
#initializers/elasticsearch.rb
# ...
config = {
host: ENV.fetch('AWS_ELASTICSEARCH_URL'),
port: 443,
scheme: "https",
retry_on_failure: true,
transport_options: {
request: { timeout: 10 }
}
}
client = Elasticsearch::Client.new( config ) do |f|
# ...
N.B. AWS_ELASTICSEARCH_URL must return a URL without protocol.
This is because of version issue.
Use this gem 'elasticsearch-model', '~> 5'

rails i18n redis ERR unknown command [] railscast 256

Following this RailsCast : http://railscasts.com/episodes/256-i18n-backends but using Rails 5.2, I raise this error :
Redis::CommandError in Pages#home<br>
ERR unknown command '[]'
In config/initializers/i18n_backend.rb
TRANSLATION_STORE = Redis.new seems causing this problem.
Whereas TRANSLATION_STORE = {} works like a charm.
But without Redis!
Any hint?
The problem is defined here:
https://github.com/ruby-i18n/i18n/blob/master/lib/i18n/backend/key_value.rb#L25-L30
I haven't investigated redis 4 but it seems that these methods has been removed.
The solution is to patch the Redis gem and add these methods to the redis.
# config/initializers/redis.rb
class RedisHash
def initialize(redis)
#redis = redis
end
def [](key)
#redis.get(key)
end
def []=(key, value)
#redis.set(key, value)
end
end
Redis.current = Redis.new(host: 127.0.0.1,
port: 6379,
db: 0,
thread_safe: true)
# config/initializers/i18n.rb
I18n::Backend::KeyValue.new(RedisHash.new(Redis.current))
This code above is sample initializer. It works with the newest version of redis 4.3.5
I've tested also redis-store/redis-i18n and it also works with the newest redis versions but in my opinion this implementation enforces huge overload on redis.
EDIT:
Due to the redis contributors [answer][1]
[1]: https://github.com/redis/redis-rb/issues/997#issuecomment-871302883 I've updated my solution
Not a full answer I know, but I had a similar problem after upgrading redis gem from 3.3.1 to 4.0.2
I set it back to 3.3.1 and it got fixed. Odd thing for me was that the problem only occurred in the production environment.
I am using a chained backend
I18n.backend= I18n::Backend::Chain.new(
I18n::Backend::KeyValue.new(Redis.current),
I18n.backend
)

Running API/gem on heroku gives Load error

I use the payment provider Mollie in my Ruby on Rails app. It works locally but not on heroku. When I call the API, it gives the error: 500: Load error: cannot load such file -- Mollie/Api/Client. Does someone know how to load the API on heroku? Update: I think that it has something to do with the location where heroku saves the gems. And that it can't find the location of the gem (??)
I installed the gem locally by running
$ gem install mollie-api-ruby
I also added the gem to my gemfile (but don't know if this is necessary).
gem 'mollie-api-ruby'
I am not experienced in Ruby on Rails and Heroku, but I hope that someone has a tip to get this working. Thanks!
I request this API in a controller by:
def create
#thrill = Thrill.find(params[:thrill_id])
if #thrill.reservations.length < #thrill.training.tr_max_attendants
#reservation = current_user.reservations.create(reservation_params)
if #reservation
require 'Mollie/API/Client'
mollie = Mollie::API::Client.new('test_gUejkz43UkdeCauC22J6UNq')
payment = mollie.payments.create(
amount: #reservation.thrill.training.tr_price,
description: #reservation.thrill.training.tr_name,
redirect_Url: 'http://8a3a73cb.ngrok.io/your_trips',
webhookUrl: 'http://8a3a73cb.ngrok.io/notify',
metadata: {
reservationid: #reservation.id
}
)
#reservation.update_attributes payid:payment.id
redirect_to payment.payment_url
else
redirect_to #thrill.training, notice: "Helaas, de training is vol"
end
end
end
Ok, found the answer. It had to be lowercase : require 'mollie/api/client'

Server not available on localhost with response code 403 & RuntimeError in Ruby on Rails

After changing proxy settings in open_uri.rb and server_manage.rb I finally managed to install neo4j behind a proxy server. The neo4j server is running on port 7000 ( It opens in the browser) but when i enter :
$rails generate scaffold post title body
Error:
/.rvm/gems/ruby-2.2.3/gems/neo4j-core-5.1.6/lib/neo4j-server/cypher_session.rb:51:in `open': Server not available on http://localhost:7000 (response code 403) (RuntimeError)
What should I do ?
Any help is appreciated!!
$ ruby --version
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
$ rails --version
Rails 4.2.2
My guess - proxy issues. Things may behave differently in your browser and code (because those are 2 different environment).
To check what exactly is going on with your database, you should try to make request to Neo4j manually, from command line.
Example with using curl:
# if auth enabled
curl -i --user username:password http://localhost:7000/db/data/
# if auth disabled
curl -i http://localhost:7000/db/data/
This will give you more details on what exactly is not working.
Also you can assemble basic ruby script that will make HTTP request, to check what you receive in response in this case.
A 403 might mean that your Neo4j authentication credentials are wrong. See http://neo4jrb.readthedocs.org/en/5.1.x/Setup.html#rails-configuration for details but basically, adding something like this to application.rb might do the trick:
config.neo4j.session_options = { basic_auth: { username: 'foo', password: 'bar'} }
Also, since you mentioned needing help with the proxy, you can add an initialize key to set that.
init = { proxy: { uri: 'http://myproxy', user: 'username', password: 'password' }}
auth = { username: 'neo4j', password: 'pwhere'}
config.neo4j.session_options = { basic_auth: auth, initialize: init }

Resources