# Elasticsearch php api with multiple hosts

**URL:** <https://discuss.elastic.co/t/elasticsearch-php-api-with-multiple-hosts/19319>\
**Category:** Elasticsearch\
**Created:** [August 18, 2014, 11:52am UTC](https://discuss.elastic.co/t/elasticsearch-php-api-with-multiple-hosts/19319 "2014-08-18T11:52:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niv\_Penso](https://avatars.discourse-cdn.com/v4/letter/n/b487fb/32.png) [@Niv\_Penso](https://discuss.elastic.co/u/Niv_Penso)\
**Post date:** [August 18, 2014, 11:52am UTC](https://discuss.elastic.co/t/elasticsearch-php-api-with-multiple-hosts/19319/1 "2014-08-18T11:52:31Z")

</div>

I followed this link to create an elasticsearch 2 nodes cluster on Azure: this  
link [http://thomasardal.com/running-elasticsearch-in-a-cluster-on-azure/](http://thomasardal.com/running-elasticsearch-in-a-cluster-on-azure/)

the installation and configuring went good.

When i started to check the cluster i found a strange behaviour from the  
php client.

I declared 2 hosts in the client:

$ELSEARCH\_SERVER = array("dns1:9200","dns2:9200");  
$params = array();  
$params['hosts'] = $ELSEARCH\_SERVER;  
$dstEl = new Elasticsearch\Client($params);

the excpected behaviour is that it will try to insert the documents to  
"dns1" and if it fails it will _automatically_ change to "dns2". but, for  
some reason when one of the servers is down on insertion the php client  
throws an exception that it couldn't connect to host and only.

Is there any way to cause the client automatically choose an online server?

thnx, Niv

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/7489eb44-bff3-41d1-baa1-da70b508ef66%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7489eb44-bff3-41d1-baa1-da70b508ef66%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Niv\_Penso](https://avatars.discourse-cdn.com/v4/letter/n/b487fb/32.png) [@Niv\_Penso](https://discuss.elastic.co/u/Niv_Penso)\
**Post date:** [August 20, 2014, 3:59pm UTC](https://discuss.elastic.co/t/elasticsearch-php-api-with-multiple-hosts/19319/2 "2014-08-20T15:59:42Z")

</div>

Solution: After debugging the the elasticsearch php client I found a retry  
mechansim that allows the client to jump to the next server from the pool  
if the previous is down.  
**this mechanism is disabled by default**

to enable this mechanism you need to initialize the Client with a parameter  
called retries:

```
$params = array();
$params['hosts'] = $ELSEARCH_SERVER;    
$params['retries'] = count($ELSEARCH_SERVER);
$dstEl = new Elasticsearch\Client($params);

```

Hope it will help you guys

On Monday, August 18, 2014 2:52:31 PM UTC+3, Niv Penso wrote:

> I followed this link to create an elasticsearch 2 nodes cluster on Azure: this  
> link [http://thomasardal.com/running-elasticsearch-in-a-cluster-on-azure/](http://thomasardal.com/running-elasticsearch-in-a-cluster-on-azure/)
> 
> the installation and configuring went good.
> 
> When i started to check the cluster i found a strange behaviour from the  
> php client.
> 
> I declared 2 hosts in the client:
> 
> $ELSEARCH\_SERVER = array("dns1:9200","dns2:9200");  
> $params = array();  
> $params['hosts'] = $ELSEARCH\_SERVER;  
> $dstEl = new Elasticsearch\Client($params);
> 
> the excpected behaviour is that it will try to insert the documents to  
> "dns1" and if it fails it will _automatically_ change to "dns2". but, for  
> some reason when one of the servers is down on insertion the php client  
> throws an exception that it couldn't connect to host and only.
> 
> Is there any way to cause the client automatically choose an online server?
> 
> thnx, Niv

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/73467675-14c2-497d-8934-67b31e5b6295%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/73467675-14c2-497d-8934-67b31e5b6295%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 1:07am UTC](https://discuss.elastic.co/t/elasticsearch-php-api-with-multiple-hosts/19319/3 "2017-07-06T01:07:24Z")

</div>


