1

我需要在elasticsearch中索引多个json,索引id应该由用户给出,而不是由elasticserach自动创建。

任何人都可以告诉我如何阻止 elasticsearch 创建自动索引 id 以及如何使用我想要的 id 进行数据索引。

下面是用于索引数据的 node.js 代码的一部分:

elasticSearchClient.index('index_name', 'type', json)
                .on('data', function(data) {
                    console.log("************ "+data+" ****************")
                })
                .exec()

任何帮助将不胜感激!

问候

4

4 回答 4

2

只需id在您的 json 文档中包含该字段。它将自动从文档中提取并放入 url。实际上core.js脚本包含这个逻辑:

if (document.id) {
    path += "/" + document.id
    method = 'PUT'
    delete document.id
}
于 2012-10-03T09:19:27.903 回答
2

如果我们不提供索引 ID,则文档的索引 ID 将由 elasticsearch 自动创建。

所以我使用下面的代码并告诉索引文档的索引ID。

var commands = []
 commands.push({ "index" : { "_index" :'opt', "_type" : "art", "_id":my_id} })
 commands.push(index_ingjson_doc)

 elasticSearchClient.bulk(commands, {})
                            .on('data', function(data) {
                            }).on('error', function(error){})
                            .exec();

这样,我的问题就解决了!其他一些解决方案可能是可能的,但到目前为止我正在使用上面的代码。

于 2012-10-17T07:43:10.647 回答
1

如果我对您的理解正确,只需"_id": whatever输入您的 JSON 并确保index.mapping._id.indexed设置为true.

于 2012-10-03T07:56:26.937 回答
1

elasticSearchClient.bulk() 是我的问题的解决方案。

参考: https ://github.com/phillro/node-elasticsearch-client

于 2012-10-15T08:51:30.237 回答