Elasticsearch本地运行时ping请求返回400错误,其他操作正常如何解决?
解决Elasticsearch Ping请求400 Bad Request问题
嘿,我一眼就揪出问题所在啦!你代码里的ping请求参数拼写出错了——把requestTimeout写成了requestTimeOut(注意第二个字母T的大小写,正确的应该是小写t)。
Elasticsearch的API只认正确的参数名requestTimeout,当你传递错误的requestTimeOut时,它会返回400 Bad Request,这就是ping失败的根源。而你其他操作(创建索引、集群健康查询等)没用到这个错误参数,所以能正常执行。
修正后的代码
把ping方法里的参数名改回正确的写法就行:
function es() { throw new Error('Looks like you are expecting the previous "elasticsearch" module. ' + 'It is now the "es" module. To create a client with this module use ' + '`new es.Client(params)`.'); } es.Client = require('./lib/client'); es.ConnectionPool = require('./lib/connection_pool'); es.Transport = require('./lib/transport'); es.errors = require('./lib/errors'); module.exports = es; var elasticsearch = require('elasticsearch') var client = new es.Client({ host: 'localhost:9200', log: 'trace', }) // Ping the cluster client.ping({ // 修正参数名:requestTimeOut → requestTimeout requestTimeout: 30000, }, function(error){ if(error) { console.log(error) console.error("elasticsearch cluster is down!") } else { console.log("All is well") } })
验证效果
修改后重新运行$node elasticsearch.js,ping请求应该会正常返回"All is well",因为现在传递的是Elasticsearch能识别的正确参数了。
内容的提问来源于stack exchange,提问作者Chandrika
相关产品推荐
相关产品推荐

