如何检查ElasticSearch客户端是否连接?

我正在使用elasticsearch-js (NodeJS),只要ElasticSearch正在运行,一切正常。 但是,我想知道我的连接在尝试调用其中一个客户端方法之前是处于活动状态。 我以同步的方式做了一些事情,但只是为了性能testing的目的(例如,检查我是否有一个空索引工作,摄取一些数据,查询数据)。 看这样的片段:

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"}); /// ... 

是否有一些机制发送到客户端configuration快速失败,或者我可以在客户端上执行一些testing,以确保它在调用delete之前打开?

更新:2015-05-22
我不确定这是否正确,但也许尝试获取客户端统计信息是合理的?

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

通过node-debug,当ElasticSearch被拒绝/无法访问时,我看到拒绝的承诺: "Error: No Living connections" 。 当它连接时,我的处理程序中的o似乎有关于连接状态的详细信息。 这种方法是正确的还是有一个首选的方法来检查连接可行性?

获取统计数据可能是一个沉重的电话,以确保您的客户端连接。 你应该使用ping,见第二个例子https://github.com/elastic/elasticsearch-js#examples

在启动elasticsearch-js客户端连接之后,我们也使用了ping。

 // 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'); } });