ruby-on-railsrubyelasticsearchelasticsearch-ruby

where to specify cluster details when using elastic search gem in Ruby


I want to access the data in Elastic Search Cluster from my rails application. Lets say server is running at http://localhost:9200 and I want to access the end point http://localhost:9200/location/type.

The following this documentation and came across this example:

require 'elasticsearch'

client = Elasticsearch::Client.new log: true

client.cluster.health

client.index index: 'my-index', type: 'my-document', id: 1, body: { title: 'Test' }

client.indices.refresh index: 'my-index'

client.search index: 'my-index', body: { query: { match: { title: 'test' } } }

Questions:


Solution

  • As the documentation specifics, the elasticsearch gem wraps elasticsearch-transport for connecting to a cluster and elasticsearch-api for accessing the elasticsearch API. From the documentation of elasticsearch-transport,

    In the simplest form, connect to Elasticsearch running on http://localhost:9200 without any configuration:

    So basically, client = Elasticsearch::Client.new log: true will by default connect to the cluster running at localhost:9200 (the same machine as your Rails app).

    Go ahead and try executing client.cluster.health after establishing the connection and you'll get to know if it succeeded or not.

    Moreover, if your cluster runs on a different server, you can use the following to connect to it:

    es = Elasticsearch::Client.new host: http(s)://<path-to-cluster-server>