我正在尝试通过elasticsearch存储所有Json对象。
client.create({
index: 'index',
type: 'type',
id:"1"
body:result[0]
},function (error,response)
{
if (error)
{
console.log('elasticsearch cluster is down!');
}
else
{
console.log('All is well');
}
});
在这个
result[0]
中,我获得了Json对象的第一个值,但是我需要动态存储所有Json对象。我得到的输出是:
-> POST http://localhost:9200/index/type/1?op_type=create
{
"Name": "Martin",
"Age": "43",
"Address": "trichy"
}
<- 201
{
"_index": "index",
"_type": "type",
"_id": "1",
"_version": 4,
"created": true
}
但是我需要这样的输出:
-> POST http://localhost:9200/index/type/1?op_type=create
{
"Name": "Martin",
"Age": "43",
"Address": "trichy"
},
{
"Name": "vel",
"Age": "23",
"Address": "chennai"
},
{
"Name": "ajay",
"Age": "23",
"Address": "chennai"
}
<- 201
{
"_index": "index",
"_type": "type",
"_id": "1",
"_version": 4,
"created": true
}
最佳答案
您需要使用bulk endpoint以便同时发送许多文档。
主体在每个文档中包含两行,第一行包含文档的索引,类型和ID,文档本身位于下一行。冲洗并重复每个文档。
client.bulk({
body: [
// action description
{ index: { _index: 'index', _type: 'type', _id: 1 } },
// the document to index
{ Name: 'Martin', Age: 43, Address: 'trichy' },
{ index: { _index: 'index', _type: 'type', _id: 2 } },
{ Name: 'vel', Age: 23, Address: 'chennai' },
{ index: { _index: 'index', _type: 'type', _id: 3 } },
{ Name: 'ajay', Age: 23, Address: 'chennai' }
]
}, function (err, resp) {
// ...
});
我怀疑您的
result
数组是昨天从your other question获得的JSON。如果是这样,那么您可以动态地构建主体,如下所示:var body = [];
result.forEach(function(row, id) {
body.push({ index: { _index: 'index', _type: 'type', _id: (id+1) } });
body.push(row);
});
然后,您可以在
body
调用中使用bulk
,如下所示:client.bulk({
body: body
}, function (err, resp) {
// ...
});
关于json - Elasticsearch:如何动态存储所有json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32372608/