如何为elasticsearch做create_or_update?

我有一个简单的例子:

var client = new elasticsearch.Client({ host: 'localhost:9200' }); client.create({ index: 't1', type: 't2', id : id, body: row},function(err,results) { callback(err, results ) }) 

如果logging存在,则结果错误:DocumentAlreadyExistsException。 如果logging存在,如何更新logging?

尝试client.index而不是client.create 。 请记住,这将始终完全用给定的idreplace您的现有文档。

在update方法中有一个属性 – doc_as_upsert: true

试试下面的代码

 var param = { index: 't1', type: 't2', id : id, body: {doc:row, doc_as_upsert: true }}; client.update(param,function(err,results) { callback(err, results ) })