簡體   English   中英

節點Elasticsearch - 批量索引不起作用 - 不支持Content-Type標頭[application / x-ldjson]

[英]Node Elasticsearch - Bulk indexing not working - Content-Type header [application/x-ldjson] is not supported

作為elasticsearch的新手,我正在通過與節點集成並嘗試在Windows中執行以下在線git示例來探索它。

https://github.com/sitepoint-editors/node-elasticsearch-tutorial

在嘗試從data.json導入1000個項目的數據時,執行'node index.js'失敗,出現以下錯誤。

通過啟用跟蹤,我現在將以下內容視為批量函數的根本原因。

"error": "Content-Type header [application/x-ldjson] is not supported",
** "status": 406**

我在https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/changelog.html上看到了更改日志

13.0.0(2017年4月24日)發送行分隔JSON主體的批量和其他API現在使用Content-Type:application / x-ndjson標頭#507

知道如何在index.js中解決此內容類型問題?

index.js

 (function () {
  'use strict';

  const fs = require('fs');
  const elasticsearch = require('elasticsearch');
  const esClient = new elasticsearch.Client({
    host: 'localhost:9200',
    log: 'error'
  });

  const bulkIndex = function bulkIndex(index, type, data) {
    let bulkBody = [];

    data.forEach(item => {
      bulkBody.push({
        index: {
          _index: index,
          _type: type,
          _id: item.id
        }
      });      
      bulkBody.push(item);
    });

   esClient.bulk({body: bulkBody})
    .then(response => {
      console.log(`Inside bulk3...`);
      let errorCount = 0;
      response.items.forEach(item => {
        if (item.index && item.index.error) {
          console.log(++errorCount, item.index.error);
        }
      });
      console.log(`Successfully indexed ${data.length - errorCount} out of ${data.length} items`);
    })
    .catch(console.err);
  };

  // only for testing purposes
  // all calls should be initiated through the module
  const test = function test() {
    const articlesRaw = fs.readFileSync('data.json');
    const articles = JSON.parse(articlesRaw);
    console.log(`${articles.length} items parsed from data file`);
    bulkIndex('library', 'article', articles);
  };

  test();

  module.exports = {
    bulkIndex
  };
} ());

我的本地Windows環境:

java版本1.8.0_121
elasticsearch版本6.1.1
節點版本v8.9.4
npm版本5.6.0

bulk函數不返回承諾。 它接受回調函數作為參數。

esClient.bulk(
  {body: bulkBody},
  function(err, response) {
    if (err) { console.err(err); return; }
    console.log(`Inside bulk3...`);
    let errorCount = 0;
    response.items.forEach(item => {
      if (item.index && item.index.error) {
        console.log(++errorCount, item.index.error);
      }
    });
    console.log(`Successfully indexed ${data.length - errorCount} out of ${data.length} items`);
  }
)

或使用promisify將接受(err, value) => ...樣式回調的函數轉換為返回promise的函數。

const esClientBulk = util.promisify(esClient.bulk)
esClientBulk({body: bulkBody})
  .then(...)
  .catch(...)

編輯:剛剛發現elasticsearch-js 支持回調和承諾。 所以這應該不是問題。

通過查看您鏈接的項目的package.json ,它使用elasticsearch-js版本^11.0.1這是一個舊版本,並且發送帶有application/x-ldjson標頭的請求用於批量上傳,這不是由更新的彈性搜索版本支持 因此,將elasticsearch-js升級到更新版本(當前最新版本為14.0.0 )應該可以修復它。

暫無
暫無

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

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