2012-10-03 26 views
1

我需要在elasticsearch中索引多個jsons,並且索引id應該由用戶給出而不是由elasticserach自動創建。node.js:通過使用用戶定義的id在elasticsearch中進行數據索引

可以任何請告訴我如何停止elasticsearch創建自動索引id和如何使用我想要的id爲數據索引。

下面是Node.js的代碼索引數據的一部分:

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

任何幫助將不勝感激!

問候

回答

1

如果我正確理解你的,只是把"_id": whatever到您的JSON和確保index.mapping._id.indexed設置爲true

2

只需在您的json文檔中包含id字段。它會自動從文檔中提取並放入網址中。事實上,core.js腳本包含這樣的邏輯:

if (document.id) { 
    path += "/" + document.id 
    method = 'PUT' 
    delete document.id 
} 
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(); 

這樣,我解決了我的問題!一些其他的解決方案可能是可能的,但到目前爲止我正在使用上述代碼。

相關問題