elasticsearch,bulk-load,Json,Node.js,elasticsearch,Bulk Load" /> elasticsearch,bulk-load,Json,Node.js,elasticsearch,Bulk Load" />

Json Elasticsearch js批量索引类型错误 背景

Json Elasticsearch js批量索引类型错误 背景,json,node.js,elasticsearch,bulk-load,Json,Node.js,elasticsearch,Bulk Load,最近,我一直在使用API对一个大型JSON文件进行大容量索引。我已经成功解析了JSON文件。现在,我应该能够将索引就绪数组传递到Elasticsearch批量命令中。但是,使用控制台日志,似乎阵列不应该引起任何问题 错误代码 下面的代码应该获取一个API URL(带有JSON响应)并使用Node.js HTTP库对其进行解析。然后使用API,它应该将JSON数组中的每个条目批量索引到我的Elasticsearch索引中 var APIUrl = /* The url to the JSON fi

最近,我一直在使用API对一个大型JSON文件进行大容量索引。我已经成功解析了JSON文件。现在,我应该能够将索引就绪数组传递到Elasticsearch批量命令中。但是,使用控制台日志,似乎阵列不应该引起任何问题

错误代码 下面的代码应该获取一个API URL(带有JSON响应)并使用Node.js HTTP库对其进行解析。然后使用API,它应该将JSON数组中的每个条目批量索引到我的Elasticsearch索引中

var APIUrl = /* The url to the JSON file on the API providers server. */
var bulk = [];

/*
  Used to ready JSON file for indexing
*/
var makebulk = function(ParsedJSONFile, callback) {
    var JSONArray = path.to.array; /* The array was nested... */
    var action = { index: { _index: 'my_index', _type: 'my_type' } };

    for(const item of items) {
        var doc = { "id": `${item.id}`, "name": `${item.name}` };

        bulk.push(action, doc);
    }
    callback(bulk);
}

/*
  Used to index the output of the makebulk function
*/
var indexall = function(madebulk, callback) {
    client.bulk({
        maxRetries: 5,
        index: "my_index",
        type: "my_type",
        body: makebulk
    }, function(err, resp) {
        if (err) {
           console.log(err);
        } else {
           callback(resp.items);
        }
    });
}

/* 
   Gets the specified URL, parses the JSON object, 
   extracts the needed data and indexes into the 
   specified Elasticsearch index
*/
http.get(APIUrl, function(res) {
   var body = '';

   res.on('data', function(chunk) {
       body += chunk;
   });

   res.on('end', function() {
       var APIURLResponse = JSON.parse(body);

       makebulk(APIURLResponse, function(resp) {
           console.log("Bulk content prepared");

           indexall(resp, function(res) {
              console.log(res);
           });
           console.log("Response: ", resp);
      });
   });
}).on('error', function(err) {
    console.log("Got an error: ", err);
});
在web服务器上运行
node bulk_index.js
时,我收到以下错误:
TypeError:bulk body应该是命令数组/字符串,或者是字符串。但是,这没有任何意义,因为
console.log(res)
命令(来自
http.get
client请求下的
indexall
函数)输出以下内容:

Bulk content prepared
Response:  [ { index: { _index: 'my_index', _type: 'my_type', _id: '1' } },
  { id: '5', name: 'The Name' }, ... },
  ... 120690 more items ]
上面的控制台输出以正确的格式显示数组

问题: 什么是
TypeError:Bulk body应该是一个命令/字符串数组,或者一个字符串
表明我传递到
客户机.Bulk
函数的数组有问题

笔记
我的服务器目前正在运行Elasticsearch
6.2.4
和Java开发工具包版本
10.0.1
。就Elaticsearch服务器甚至我的Elaticsearch映射而言,一切都可以正常工作(我没有提供
client.index.putMapping
code,但是如果需要,我可以提供)。我已经花了好几个小时阅读了我能找到的关于这个类型错误的每一份文档。关于抛出的错误,我找不到太多信息,因此我不确定还有什么地方可以查找有关此错误的信息。

您的代码似乎有误

var indexall = function(**madebulk**, callback) {
    client.bulk({
        maxRetries: 5,
        index: "my_index",
        type: "my_type",
        body: **makebulk**

检查madebulk和makebulk。

似乎是您的代码中的一个输入错误

var indexall = function(**madebulk**, callback) {
    client.bulk({
        maxRetries: 5,
        index: "my_index",
        type: "my_type",
        body: **makebulk**
检查madebulk和makebulk