elasticsearch执行sql查询接口报405错误
in nodejsElasticsearch with 0 comment

elasticsearch执行sql查询接口报405错误

in nodejsElasticsearch with 0 comment

报错信息:

client create error { ResponseError: Response Error
    at IncomingMessage.response.on (/Users/kaseisai163.com/www/zhiyuanhui/cd-metro-ms-es/node_modules/@elastic/elasticsearch/lib/Transport.js:302:25)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:139:11)
    at process._tickCallback (internal/process/next_tick.js:181:9)
  name: 'ResponseError',
  meta: 
   { body: 
      { error: 'Incorrect HTTP method for uri [/_sql?format=json] and method [POST], allowed: [GET, DELETE, HEAD, PUT]',
        status: 405 },
     statusCode: 405,
     headers: 
      { allow: 'GET,DELETE,HEAD,PUT',
        'content-type': 'application/json; charset=UTF-8',
        'content-length': '127' },
     warnings: null,
     meta: 
      { context: null,
        request: [Object],
        name: 'elasticsearch-js',
        connection: [Object],
        attempts: 0,
        aborted: false } } }

执行该操作的代码:

const config = require("config");
const { Client } = require("@elastic/elasticsearch");
const client = new Client({ node: "localhost:9200" });

client.sql.query(
  {
    //index: "twitter", // 索引
    //id: 2,              // id
    //type: "doc", // 类型
    format: "txt",
    method: "post",
    body: {
      query: "SELECT * FROM twitter"
    }
  },
  {
    headers: {
      "content-type": "application/json"
    }
  },
  function(error, data) {
    if (error) {
      console.error("client create error", error);
    } else {
      console.log("client create success ", data);
    }
  }
);

解决方法

这个问题是版本引起的,当时我的es版本是6.3.1,但是使用的@elastic/elasticsearch模块,官方的解释是该模块是适用于es7.2版本的,所以将es和kibana的版本升级就好了

谨记:很多时候es的问题都是版本造成的

Responses