how to check my elasticSearch client connection

3,621 views
Skip to first unread message

Alexandre Moros

unread,
Oct 26, 2015, 4:59:06 AM10/26/15
to Elastica - Elasticsearch PHP Client
Hi, 
I have one slight probleme.
When I'm running my application without my elasticsearch server launch I have major issues.
When i try to check myClient->hasConnection() it says it's ok but when i try an operation (like adddocument) on that client my application stops runnning.
I have no idea of an other way to check if my elasticsearch server is alive :/

Regards
Message has been deleted

ruflin

unread,
Oct 26, 2015, 4:57:24 PM10/26/15
to Elastica - Elasticsearch PHP Client
@Alexander: I just briefly checked the code, and this is expected. The reason for this is that it is just checking of the connection object is enabled, not if the connection is working. HasConnection checks if there is at least 1 connection object.

A potential solution is

try {
   $client->getStatus();
} catch(Exception ...) {
   ... do stuff
}

So before you start your application, you check for the status which will throw an exception, in case the connection doesn't work.



On Monday, October 26, 2015 at 9:53:27 PM UTC+1, ruflin wrote:
That is of course not expected. What version of Elastica and Elasticsearch are you using?

Alexandre Moros

unread,
Oct 27, 2015, 4:08:06 AM10/27/15
to Elastica - Elasticsearch PHP Client
Hi
Thanks for helping me :)
I try your above solution but the problems remains. when i try this 

try {
            error_log("1", 0);
            $client->getStatus();
            error_log("2", 0);
        } catch(Exception $e) {
            error_log("3", 0);
        }

I've still my internal server error raised and only the 1 is log on error.php :(. In fact i can't do anything on my client without raising this error and can't catch it
I'm using the dev-master version of elastica

ruflin

unread,
Oct 27, 2015, 4:15:50 AM10/27/15
to Elastica - Elasticsearch PHP Client
That is somehow strange. What output do you have in the php error log besides the message you write there on your own?

Dev-Master should be fine to use. Which elasticsearch version are you using?

Based on your comment about dev-master I just realised the link in the composer.json file should be updated ...

Alexandre Moros

unread,
Oct 27, 2015, 4:39:11 AM10/27/15
to Elastica - Elasticsearch PHP Client
here is the php error log

[27-Oct-2015 09:37:47 Europe/Paris] 1
[27-Oct-2015 09:37:47 Europe/Paris] {"path":"_status","method":"GET","data":[],"query":[],"connection":{"config":{"url":null},"host":"localhost","port":"9200","path":null,"proxy":null,"transport":null,"persistent":true,"timeout":null,"connections":[],"roundRobin":false,"log":true,"retryOnConflict":0,"enabled":true},"error_message":"logging Request"}

I use elasticSearch 1.7.2

ruflin

unread,
Oct 27, 2015, 4:52:29 AM10/27/15
to Elastica - Elasticsearch PHP Client
I'm trying to get my head around on what is happening here. From where is the second error line coming? It seems like exceptions are coverted to errors? What is your error handling setting?


Which PHP version are you using?

Can you post the full code that is needed for the above?

Alexandre Moros

unread,
Oct 27, 2015, 5:00:06 AM10/27/15
to elastica-...@googlegroups.com
the first line is my error_log debug
The second is log by the client here is my full code

class ElasticIndex 
{

    private $elasticaClient;

    function __construct() 
    {
        $this->elasticaClient = new \Elastica\Client(array(
        'host' => "localhost",
        'port' => 9200,
        'log' => true
        ));
    }

    public function hasConnection()
    {
       try {
            error_log("1", 0);
            $this->elasticaClient->getStatus();
            error_log("2", 0);
        } catch(Exception $e) {
            error_log("3", 0);
            return false;
        }
        return true;
    }

I haven't touch at my dafault setting
I use php5.5.1.2
It could be a rookie mistake because im verry new to php ^^

--
You received this message because you are subscribed to a topic in the Google Groups "Elastica - Elasticsearch PHP Client" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elastica-php-client/8YVB1aDEEyw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elastica-php-cl...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Alexandre Moros
IT Development Engineer - Atos
Linkedin

ruflin

unread,
Oct 27, 2015, 5:07:12 AM10/27/15
to Elastica - Elasticsearch PHP Client
Is this PHP 5.1.2 or PHP 5.5:*?

Alexandre Moros

unread,
Oct 27, 2015, 5:09:35 AM10/27/15
to Elastica - Elasticsearch PHP Client
5.5.*

ruflin

unread,
Oct 27, 2015, 9:09:21 AM10/27/15
to Elastica - Elasticsearch PHP Client
Hm, I somehow have the feeling I miss something :-( What OS are you on?

Can you try the following in your code. Instead of getStatus() create an index. Also remove the flag 'log' => true from the constructor.

$index = $client->getIndex('elastica_test1');
$index->create(array(), true);

Instead of error_log("3", 0); try error_log(print_r($e, true));

Alexandre Moros

unread,
Oct 27, 2015, 9:19:12 AM10/27/15
to Elastica - Elasticsearch PHP Client
I'm on windows 7

here is the new code :


    function __construct() 
    {
        $this->elasticaClient = new \Elastica\Client(array(
        'host' => Constants::HOST,
        'port' => Constants::PORT
        ));
    }

    public function hasConnection()
    {
       try {
            error_log("1", 0);
            $index = $this->elasticaClient->getIndex('elastica_test1');
            $index->create(array(), true);
            error_log("2", 0);
        } catch(Exception $e) {
            error_log(print_r($e, true), 0);
            return false;
        }
        return true;
    }

and here the log output

[27-Oct-2015 14:17:56 Europe/Paris] 1

everytime i try something on my non connected client my app just stops with the internal server error

ruflin

unread,
Oct 27, 2015, 12:19:14 PM10/27/15
to Elastica - Elasticsearch PHP Client
Oh, Windows :-) Now everything makes sense ;-)

Internal Server error is all you get? Do you have similar problems with other PHP apps on windows not throwing exceptions? Anyone here with some more Windows experience?

Alexandre Moros

unread,
Oct 28, 2015, 4:33:17 AM10/28/15
to Elastica - Elasticsearch PHP Client
well it's in a symphony app so what i got on my client side is a error 500
all i can say is that my try catch isn't working and that the only case i know even on my other app php on windows ahaha :/

ruflin

unread,
Oct 28, 2015, 6:12:39 AM10/28/15
to Elastica - Elasticsearch PHP Client
So the code you posted you run it standalone to test or always as part of the app?

Alexandre Moros

unread,
Oct 28, 2015, 6:20:34 AM10/28/15
to Elastica - Elasticsearch PHP Client
i always run it in my appas. my apps call a rest method where I search something on my elasticsearch and the first thing i do is check if my ES is alive (wich failed when is not)

Cassiano Tartari

unread,
Oct 28, 2015, 6:37:32 AM10/28/15
to elastica-php-client
Have you tried other ElasticSearch versions? Once I've configured a symfony app running under IIS with ElasticSearch and I remember some problems related to specific elasticsearch versions. Is the elasticsearch running well? http://localhost:9200/_cluster/health

Att.,
Cassiano Tartari

--
You received this message because you are subscribed to the Google Groups "Elastica - Elasticsearch PHP Client" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elastica-php-cl...@googlegroups.com.

ruflin

unread,
Oct 28, 2015, 9:16:14 AM10/28/15
to Elastica - Elasticsearch PHP Client
It would be nice if you could run the above code without the hole framework, as the framework could make a lot of additional configurations (like eating exceptions :-) ).

Alexandre Moros

unread,
Oct 28, 2015, 11:14:26 AM10/28/15
to Elastica - Elasticsearch PHP Client
OK so after some reasearch it seems to be a problem with symfony

It's now catch my exception but i needed to add an extra antislash in front of it : 

 try {
            error_log("1", 0);
            $index = $this->elasticaClient->getIndex('elastica_test1');
            $index->create(array(), true);
        } catch( \Exception $e ) {
            error_log("2", 0);
            return false;
        }
        return true;

Sorry for the scare and the troble seems to be a symfony behaviour
Thanks everyone for the help

ruflin

unread,
Oct 29, 2015, 4:28:02 AM10/29/15
to Elastica - Elasticsearch PHP Client
Ah, didn't spot that. This is not a symfony but a namespace problem. What is the error you got in the end?

Alexandre Moros

unread,
Oct 29, 2015, 4:33:38 AM10/29/15
to Elastica - Elasticsearch PHP Client
ahah told you php is not my things ^^ sorry
the error message is the following 
"Couldn't connect to host, Elasticsearch down?"

ruflin

unread,
Oct 29, 2015, 4:36:16 AM10/29/15
to Elastica - Elasticsearch PHP Client
Cool, at least this is as expected :-)
Reply all
Reply to author
Forward
0 new messages