簡體   English   中英

Elasticsearch JS批量索引TypeError

[英]Elasticsearch js Bulk Index TypeError

背景

最近,我一直在使用Elasticsearch Node.js API對大型JSON文件進行批量索引。 我已經成功解析了JSON文件。 現在,我應該能夠將索引就緒的數組傳遞給Elasticsearch bulk命令。 但是,使用控制台日志,似乎該陣列不應該引起任何問題。

錯誤的代碼

以下代碼應采用API URL(帶有JSON響應)並使用Node.js HTTP庫進行解析。 然后使用Elasticsearch Node.js 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 should either be an Array of commands/string, or a String 但是,這沒有任何意義,因為console.log(res)命令(來自http.get客戶端請求下的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 should either be an Array of commands/string, or a String指示我傳遞給client.bulk函數的數組有問題?

筆記

我的服務器當前正在運行Elasticsearch 6.2.4和Java Development Kit版本10.0.1 一切都可以運行到Elaticsearch服務器甚至我的Elaticsearch映射(我沒有提供client.indices.putMapping代碼,但是如果需要的話,我可以提供)。 我花了多個小時閱讀我可以找到的有關此TypeError的每一份文檔。 關於拋出的錯誤,我找不到太多的信息,所以我不確定在哪里可以找到有關此錯誤的信息。

您的代碼中似乎有錯字。

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

檢查makebulk和makebulk。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM