Elasticsearchを初めて使用する場合は、nodeと統合し、Windowsで次のオンラインgitの例を実行してみてください。
https://github.com/sitepoint-editors/node-elasticsearch-tutorial
data.jsonから1000項目のデータをインポートしようとすると、「nodeindex.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
回答:
回答№1は1ザ bulk
関数はpromiseを返しません。パラメータとしてコールバック関数を受け入れます。
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バージョンによる。だから、アップグレード elasticsearch-js
新しいバージョンに(現在の最新は 14.0.0
)それを修正する必要があります。