Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if ElasticSearch client is connected?

I'm working with elasticsearch-js (NodeJS) and everything works just fine as long as long as ElasticSearch is running. However, I'd like to know that my connection is alive before trying to invoke one of the client's methods. I'm doing things in a bit of synchronous fashion, but only for the purpose of performance testing (e.g., check that I have an empty index to work in, ingest some data, query the data). Looking at a snippet like this :

    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 mechanism to send in on the client config to fail fast, or some test I can perform on the client to make sure it's open before invoking delete?

Update: 2015-05-22
I'm not sure if this is correct, but perhaps attempting to get client stats is reasonable?

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

Via node-debug, I am seeing the promise rejected when ElasticSearch is down / inaccessible with: "Error: No Living connections". When it does connect, o in my then handler seems to have details about connection state. Would this approach be correct or is there a preferred way to check connection viability?

like image 426
blong Avatar asked May 22 '15 18:05

blong


1 Answers

Getting stats can be a heavy call to simply ensure your client is connected. You should use ping, see 2nd example https://github.com/elastic/elasticsearch-js#examples

We are using ping too, after instantiating elasticsearch-js client connection on start up.

// 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');
  }
});
like image 142
Julien C. Avatar answered Oct 27 '22 01:10

Julien C.