gpt4 book ai didi

node.js - Node Elasticsearch 批量索引失败且无提示

转载 作者:太空宇宙 更新时间:2023-11-04 00:00:49 24 4
gpt4 key购买 nike

使用 Nodejs 客户端与 Elasticsearch 配合使用:

  • Elasticsearch 6.6
  • Node Elasticsearch 客户端 15.4.1

我正在尝试使用此函数批量索引一堆条目:

const elasticsearch = require('elasticsearch');
const configuration = require('./configuration').elasticConfig;
const elastic = new elasticsearch.Client({
host: `${configuration.host}:${configuration.port}`
});

const elasticStore = async (inputArray) => {
try {
const insert = await elastic.bulk({body: inputArray});
console.log(insert)
} catch (e) {
logger.error('failed to insert events into elastic search', {e});
}
};

我正在以下 inputArray 上运行 elasticStore:

[
{
"index": {
"_index": "test-2019.02.19"
}
},
{
"timestamp": "2019-02-19T02:34:43.526Z",
"data": {
"something": "something"
}
},
{
"index": {
"_index": "test-2019.02.19"
}
},
{
"timestamp": "2019-02-19T02:34:43.526Z",
"data": {
"something": "something"
}
}
]

该函数在插入时失败,但它会默默地失败 - 它不会抛出错误或类似的内容。

我正在查看文档,他们的示例是这样的:

client.bulk({
body: [
// action description
{ index: { _index: 'myindex', _type: 'mytype', _id: 1 } },
// the document to index
{ title: 'foo' }
]
})

即使我运行示例,如下所示:

const insert = await elastic.bulk({body: [
// action description
{ index: { _index: 'sb-app-events-test', _type: 'mytype', _id: 1 } },
// the document to index
{ title: 'foo' }
]});

它也会默默地失败。

我认为问题可能出在我用于正文的 JSON 对象数组(尽管官方文档说这应该可以正常工作),并尝试将其转换为 JSONLines,创建一串由换行符分隔的字符串化对象,但它以同样的方式失败:

    let parsedInput ='';
inputArray.forEach(obj => parsedInput += JSON.stringify(obj) + '\n')
const insert = await elastic.bulk({body: parsedInput});

这产生了以下字符串:

"{\"index\":{\"_index\":\"myindex\",\"_type\":\"mytype\"}}\n{\"title\":\"foo\"}\n{\"index\":{\"_index\":\"myindex\",\"_type\":\"mytype\"}}\n{\"title\":\"foo\"}\n"

我错过了什么?

编辑:Duy 下面的答案解决了我的问题 - 它需要顶层和每个插入对象中的类型。由于某种原因,使用断点进行 Webstorm 调试在这里不起作用,我必须使用 console.log()。

最佳答案

检查您的代码,发现您忘记将 type 传递给 bulk 函数

# Modify const insert = await elastic.bulk({body: inputArray});
# Become
const insert = await elastic.bulk({ body: inputArray, type: 'mytype'});

使用 https://gist.github.com/duynguyenhoang/b05c1c6342f639e440422256c68fd040 进行测试而且效果很好

注意:Nodejs v8.11.1

关于node.js - Node Elasticsearch 批量索引失败且无提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54781538/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com