【问题标题】:Elastic search gives Bad request for ping弹性搜索给出了错误的 ping 请求
【发布时间】:2022-05-19 04:47:58
【问题描述】:

elasticsearch.js 文件中的代码

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: 30000,
},
function(error){
  if(error) {
      console.log(error)
      console.error("elasticsearch cluster is down!")
  } 
  else {
      console.log("All is well")
  }
})

我正在使用命令 $bin/elasticsearch

在本地运行弹性搜索

但是当我这样做时 $node elasticsearch.js 它给出了错误提示

Elasticsearch INFO: 2018-01-22T11:17:50Z
  Adding connection to http://localhost:9200/

Elasticsearch DEBUG: 2018-01-22T11:17:50Z
  starting request {
    "method": "HEAD",
    "requestTimeout": 3000,
    "castExists": true,
    "path": "/",
    "query": {
      "requestTimeOut": 30000
    }
  }


Elasticsearch TRACE: 2018-01-22T11:17:50Z
  -> HEAD http://localhost:9200/?requestTimeOut=30000

  <- 400


Elasticsearch DEBUG: 2018-01-22T11:17:50Z
  Request complete

{ Error: Bad Request
    at respond (/Users/ElasticSearchServer/node_modules/elasticsearch/src/lib/transport.js:307:15)
    at checkRespForFailure (/Users/ElasticSearchServer/node_modules/elasticsearch/src/lib/transport.js:266:7)
    at HttpConnector.<anonymous> (/Users/ElasticSearchServer/node_modules/elasticsearch/src/lib/connectors/http.js:159:7)
    at IncomingMessage.bound (/Users/ElasticSearchServer/node_modules/elasticsearch/node_modules/lodash/dist/lodash.js:729:21)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)


status: 400,
  displayName: 'BadRequest',
  message: 'Bad Request',
  path: '/',
  query: { requestTimeOut: 30000 },
  body: undefined,
  statusCode: 400,
  response: '',
  toString: [Function],
  toJSON: [Function] }

elasticsearch 集群已关闭!

如果我尝试添加新索引、删除索引、检查运行状况或搜索,它可以正常工作并给出适当的结果。 谁能帮我解决这个问题?提前致谢!

【问题讨论】:

    标签: node.js elasticsearch


    【解决方案1】:

    在新的 JavaScript 客户端中,每个不适用于 Elasticsearch 的选项都存在于第二个对象中,您的代码应按如下方式更新:

    'use strict'
    
    const { Client } = require('@elastic/elasticsearch')
    const client = new Client({ node: 'http://localhost:9200' })
    
    client.ping({}, { requestTimeout: 20000 }, (err, response) => {
     ...
    })
    

    在除正文、状态代码和标题之外的响应对象中,您还将找到一个警告数组和一个元对象,它们应该可以帮助您调试问题。 在这种情况下,警告包含以下消息:“客户端 - 未知参数:“requestTimeout”,将其作为查询参数发送”。

    【讨论】:

      猜你喜欢
      • 2019-03-07
      • 2020-11-20
      • 1970-01-01
      • 2012-11-29
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      相关资源
      最近更新 更多