2014-09-10 97 views
0

在我的节点应用程序中,我使用“Elasticsearch”进行数据检索。当我只连接到1台主机时,它工作正常。node.js elasticsearch多个主机

为前:

try { 
     elasticSearchClient = new ElasticSearchClient({ 
     host: 'xxx.xxx.x.xxxx', 
     port: '9200' 
    }); 
} catch (err) { 
    console.log('err=' + err); 
} 

但是当我试图连接到多台主机(例如)我得到错误:

对于前:

var serverOptions = { 
    hosts:[ 

     { 
      host: 'xxx.xxx.x.xxx', 
      port: 9200 
     },{ 
      host: 'xxx.xxx.x.xxx', 
      port: 9200 
     }] 
}; 

elasticSearchClient = new ElasticSearchClient(serverOptions); 

错误:

ECONN REFUSED: socket hang up 

我正在使用“elsaticsearchclient”节点包。帮我解决这个问题。在此先感谢..

编辑:

其实1主机已启动并在另一台主机宕机。所以当主机1关闭时,我不得不重定向到另一个主机..我如何实现这一目标?

回答

0

hosts应该是一个完整的URL数组。所以,做到以下几点:

elasticSearchClient = elasticsearch.Client({ 
    hosts: serverOptions.hosts.map(function (server) { 
     return 'http://' + serverOptions.user + ':' + serverOptions.password + '@' + server.host + ':' + server.port; 
    }) 
}), 
+0

Thannks。但是我们用“elasticsearch”包,但我使用“elasticsearchclient”包..所以,如果你使用上面code.i不必须。对方法.. – Subburaj 2014-09-10 11:20:59

+0

你没没有提及你使用过哪个客户端,所以我假定你使用的是官方模块,而不是第三方。我建议你这样做。 – Eye 2014-09-10 11:23:07

+0

K.我需要为第三方软件包做些什么.. – Subburaj 2014-09-10 11:25:03