在插入时设置ElasticSearch嵌套向上插入

h9a6wy2h  于 2022-10-06  发布在  ElasticSearch
关注(0)|答案(1)|浏览(163)

我有以下代码来将文档插入到我的索引中,它工作得很好:

var dtos = new PlayerDto[]
{
    new PlayerDto 
    {
        Id = "1",
        AccountId = "1",
        Name = "test"
    }
};

var response = await _elastic.BulkAsync(b => b
    .Index(indexName)
    .UpdateMany(dtos, (bu, d) => bu.Doc(d).DocAsUpsert(true))
);

但是,我不想在每次插入时都覆盖Name字段。我是否可以将Name字段设置为仅在文档插入而不是更新时设置?或者,仅当现有的Name为空时才设置Name

bwleehnv

bwleehnv1#

当然,只需将插入文档的字段设为空即可。例如如下所示:

public class PlayerDto {
  public PlayerDto(PlayerDto model) {
    Id = model.Id,
    AccountId = model.AccountId,
    Name = null 
  }    
}

var response = await _elastic.BulkAsync(b => b
.Index(indexName)
.UpdateMany(dtos, (bu, d) => bu.Doc(new PlayerDto(d)).Upsert(d)));

相关问题