How to check if ElasticSearch client is connected?

I work with elasticsearch-js (NodeJS) and everything works fine while ElasticSearch works. However, I would like to know that my connection is alive before trying to call one of the client methods. I do things a bit synchronously, but only for performance testing purposes (for example, check that I have an empty index for work, swallowing some data, requesting data). Looking at the fragment as follows:

var elasticClient = new elasticsearch.Client({ host: ((options.host || 'localhost') + ':' + (options.port || '9200')) }); // Note, I already have promise handling implemented, omitting it for brevity though var promise = elasticClient.indices.delete({index: "_all"}); /// ... 

Is there some kind of mechanism to send in the client configuration so that it does not run quickly, or any test that I can run on the client to make sure it opens before calling delete ?

Update: 2015-05-22
I'm not sure if this is correct, but maybe trying to get customer statistics is reasonable?

  var getStats = elasticClient.nodes.stats(); getStats.then(function(o){ console.log(o); }) .catch(function(e){ console.log(e); throw e; }); 

Through node-debug, I see that the promise is rejected when ElasticSearch is unavailable / unavailable: "Error: No Living connections" . When it connects, o in my handler seems to contain connection status information. Will this approach be correct or is there a preferred way to test the viability of the connection?

+7
elasticsearch
source share
1 answer

Getting statistics can be a big challenge just to get your client connected. You must use ping, see the second example https://github.com/elastic/elasticsearch-js#examples

We also use ping after creating the elasticsearch-js client instance at startup.

 // example from above link var elasticsearch = require('elasticsearch'); var client = new elasticsearch.Client({ host: 'localhost:9200', log: 'trace' }); client.ping({ // ping usually has a 3000ms timeout requestTimeout: Infinity, // undocumented params are appended to the query string hello: "elasticsearch!" }, function (error) { if (error) { console.trace('elasticsearch cluster is down!'); } else { console.log('All is well'); } }); 
+13
source share

All Articles