'Elastic search gives Bad request for ping

Code in elasticsearch.js file

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")
  }
})

and I am running elastic search locally with command $bin/elasticsearch

but when I do $node elasticsearch.js it gives the error saying

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 cluster is down!

If I try adding new index, delete index, check the health or search, it works fine and gives the appropriate result. Can anyone help me to fix the issue? thanks in advance!



Solution 1:[1]

In the new JavaScript client every option that is not intended for Elasticsearch lives in a second object, your code should be updated as follows:

'use strict'

const { Client } = require('@elastic/elasticsearch')
const client = new Client({ node: 'http://localhost:9200' })

client.ping({}, { requestTimeout: 20000 }, (err, response) => {
 ...
})

In the response object other than body, statusCode, and headers, you will also find a warnings array and a meta object, which should help you debug issues. In this case, warnings contained the following message: 'Client - Unknown parameter: "requestTimeout", sending it as query parameter'.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Muhammed Kalkan