【发布时间】:2016-01-23 07:45:21
【问题描述】:
我通过从 html 文件中读取表格创建了一个 csv 文件。我在 node.js 中编写了脚本来创建这个 csv 文件。 现在,我必须索引弹性搜索中的字段。 我不知道弹性搜索,我需要你的帮助来解决这个问题 在我的 CSV 文件中,我有 3 个字段(名称、买入、卖出)。
【问题讨论】:
标签: node.js indexing elasticsearch
我通过从 html 文件中读取表格创建了一个 csv 文件。我在 node.js 中编写了脚本来创建这个 csv 文件。 现在,我必须索引弹性搜索中的字段。 我不知道弹性搜索,我需要你的帮助来解决这个问题 在我的 CSV 文件中,我有 3 个字段(名称、买入、卖出)。
【问题讨论】:
标签: node.js indexing elasticsearch
Elasticsearch 的一个优点是在很多情况下,您只需向其输入数据,它就会为您计算出如何为它编制索引。
使用official library,您可以:
var documents = // read from csv
client.bulk({
body: documents.map(document => [
{ index: { _index: 'myindex', _type: 'mytype', _id: 'documentid' } },
// the document to index
document
]).flatten()
}).then(response => {
// ...
});
Elasticsearch 会自动为您的字段创建映射,以便您进行搜索:
client.search({
index: 'myindex',
type: 'mytype',
body: {
query: {
match: {
name: 'joe'
}
}
}
}).then(response => {
var hits = body.hits.hits;
}).catch(error => {
console.trace(error.message);
});
【讨论】: