【问题标题】:How to check if ElasticSearch client is connected?如何检查 ElasticSearch 客户端是否已连接?
【发布时间】:2015-08-04 21:37:32
【问题描述】:

我正在使用elasticsearch-js (NodeJS),只要 ElasticSearch 正在运行,一切都会正常工作。但是,在尝试调用客户端的方法之一之前,我想知道我的连接是否有效。我正在以一种同步的方式做事,但仅出于性能测试的目的(例如,检查我是否有一个空索引可以工作,摄取一些数据,查询数据)。看着这样的 sn-p:

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

是否有某种机制可以在客户端配置上发送快速失败,或者我可以在客户端上执行一些测试以确保在调用 delete 之前它已打开?

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

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

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

【问题讨论】:

标签: node.js elasticsearch


【解决方案1】:

为了确保您的客户端已连接,获取统计信息可能是一项繁重的工作。您应该使用 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');
  }
});

【讨论】:

  • 我认为这已经过时了
  • 在 elasticsearch-py 中,这可能看起来像 elasticsearch_conn.ping(request_timeout=20000)。
猜你喜欢
  • 2018-06-11
  • 2013-07-27
  • 1970-01-01
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 2011-09-20
  • 1970-01-01
相关资源
最近更新 更多