【问题标题】:ElasticSearch NEST Upsert with set on insert插入时设置的 ElasticSearch NEST Upsert
【发布时间】:2022-09-25 21:05:38
【问题描述】:

我有以下代码可以将文档插入到我的索引中,效果很好:

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

    标签: c# elasticsearch nest upsert


    【解决方案1】:

    当然,只需将 upserting 文档的字段设为 NULL。例如像这样:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-17
      • 2015-08-18
      • 2022-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多