node.js:通过使用用户定义的id在elasticsearch中进行数据索引

我需要在elasticsearch中索引多个jsons,索引id应该由用户自己创build而不是由elasticserach自动创build。

可以任何请告诉我如何停止elasticsearch创build自动索引id,我怎样才能使用我想要的数据索引编号。

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

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

任何帮助将不胜感激!

问候

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

 if (document.id) { path += "/" + document.id method = 'PUT' delete document.id } 

如果我们不给索引id,那么索引文件的id将由elasticsearch自动创build。

所以我使用下面的代码,并告诉索引文件的索引编号。

 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(); 

这样,我解决了我的问题! 一些其他的解决scheme可能是可能的,但现在我正在使用上面的代码。

如果我正确地理解了你,只要把"_id": whatever放到你的JSON中,并确保index.mapping._id.indexed被设置为true

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

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