如何为elasticsearch做create_or_update?

Bdf*_*dfy 1 node.js elasticsearch

我有一个简单的例子:

var client = new elasticsearch.Client({
    host: 'localhost:9200'
});

  client.create({ index: 't1', type: 't2', id : id, body: row},function(err,results) {
            callback(err, results )
  })
Run Code Online (Sandbox Code Playgroud)

如果记录存在,则结果错误:DocumentAlreadyExistsException.如果记录存在,如何更新记录?

小智 10

在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 )
})
Run Code Online (Sandbox Code Playgroud)