3

I have 2 elasticsearch clusters (not nodes) I would like to access using the python official api.

I have changed Cluster lines in config files of each elasticsearch clusters:

cluster.name: elasticsearch_prod
cluster.name: elasticsearch_traf

But I can't find a way to connect to the second cluster, both are running (locally).

It seems possible to pass parameters to the Elasticsearch client constructor in python, http://www.elasticsearch.org/blog/unleash-the-clients-ruby-python-php-perl/

and the elasticsearch-python documentation eplains how to connect to a specific node or host, but not to a specific cluster: http://elasticsearch-py.readthedocs.org/en/master/api.html#elasticsearch

es = Elasticsearch(cluster='elasticsearch_traf')

leads to

In [86]: es.cluster.stats()
Out[86]: {u'cluster_name': u'elasticsearch_prod', [...]

I would like to have my clusters on different machines, and thus I prefer not to use different indexes.

Thanks

EDIT : I also tried connecting to nodes from wished cluster, but I can't make it work. My two running nodes have these names (auto :) )

'Jean Grey-Summers' (cluster_name: elasticsearch_prod)
'Sluk' (cluster_name: elasticsearch_traf)

But when I try:

es = Elasticsearch(['Jean Grey-Summers'],sniff_on_start=True)

or

es = Elasticsearch(['Sluk'], sniff_on_start=True)

It comes out with:

TransportError: TransportError(N/A, 'Enable to sniff hosts.')

The N/A makes me think I'm not doing it right...

As said in comments I'm not looking for tribe I think, I just want two clients to connect each to one different cluster running locally

Thibaut
  • 133
  • 1
  • 10
  • (since the comment was deleted : the link was : http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-tribe.html) Thanks, I've seen this one, but it seems like tribe is used to make different clusters act like there was only one, and thus allow the client thinks he's connected to 1 cluster when there are in fact 2. I wan't the clusters to be seen as different clusters (with 2 clients) – Thibaut May 15 '14 at 14:40
  • and what about this link : http://elasticsearch-py.readthedocs.org/en/master/connection.html?highlight=transport#elasticsearch.Transport.add_connection ? – Freelancer May 15 '14 at 14:45
  • I can't see the link between my question and connection ... From what I understand adding a connection only helps you open more 'pipe' to a given host, and thus cluster, but won't help me connect to 2 different clusters – Thibaut May 15 '14 at 14:48
  • Not sure how to connect to a specific cluster if multiple are available, but you can use `es.cluster.state()['cluster_name']` to check the name of the current cluster. – Tyler Oct 04 '16 at 17:50

1 Answers1

0

You should determine which port each cluster is running on and then connect like so:

from elasticsearch import Elasticsearch

# Assumes 'elasticsearch-prod' is running 9400 and 'elasticsearch-traf' is running 9200
es_traf = Elasticsearch("localhost:9200")
es_prod = Elasticsearch("http://localhost:9400")
sethmlarson
  • 923
  • 8
  • 21