【问题标题】:Set null to a document property and remove field from database when update将 null 设置为文档属性并在更新时从数据库中删除字段
【发布时间】:2019-07-29 02:08:49
【问题描述】:

我有以下 C# 类。

public class ElasticSearchDocument
{
    public string Id { get; set; } = Guid.NewGuid().ToString();

    public string Description { get; set; }
}

我也在为我的文档使用模板,下面的一个是用于演示测试的。

{
  "version": 2,
  "index_patterns": "documents-test*",
  "order": 2,
  "aliases": {
    "docs-test": {}
  },
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "_doc": {
      "dynamic": "strict",
      "properties": {
        "id": {
          "type": "keyword"
        },
        "description": {
          "enabled": false
        }
      }
    }
  }
}

我将Description 属性设置为has value 并将其编入索引。下面是数据库中的一个示例。

{
  "_index": "documents-test-2019-07-2-2",
  "_type": "_doc",
  "_id": "55096ff7-5072-4ded-b6a3-94b8e155c9d0",
  "_score": 1,
  "_source": {
    "id": "55096ff7-5072-4ded-b6a3-94b8e155c9d0",
    "description": "has value"
  }
}

查询文档,将Description 属性设置为null 并使用下面的NEST IElasticClient.UpdateAsync 方法更新文档。

public async Task<Result> UpdateAsync(
 T document,
 string indexName = null,
 string typeName = null,
 Refresh ? refresh = null,
 CancellationToken cancellationToken =
 default) {

 var response = await Client.UpdateAsync<T,
  object>(
   document.Id,
   u => u.Doc(document)
   .Index(indexName ? ? DocumentMappings.IndexStrategy)
   .Type(typeName ? ? DocumentMappings.TypeName)
   .Refresh(refresh),
   cancellationToken);

 var errorMessage = response.LogResponseIfError(_logger);

 return errorMessage.IsNullOrEmpty() ? Result.Ok() : Result.Fail(errorMessage);
}

问题是在更新命令之后,文档没有改变,description 字段的值为has value

在我看来,最合适的解决方案是将 C# 类 Description 属性设置为 null 并更新 Elastic Search 要从文档中删除的字段。

我已经看到了几个答案,但不确定可能发生了什么变化,或者是否有比 update_by_query 更好的解决方案,或者使用 property attribute 忽略 null 的覆盖行为(很麻烦)只使用 NEST

【问题讨论】:

  • 我认为在这种情况下使用[JsonProperty(NullValueHandling = NullValueHandling.Include)] 是最好的选择。
  • 我不喜欢这个选项只是因为你也不能真正查询null,使用exists的API更合适,所以我想使用它。也许使用原始客户端和 update_by_query 来删除它。

标签: c# elasticsearch nest


【解决方案1】:

我最终创建了以下方法。

public async Task<Result> UpdateAsync(
    T document, 
    string indexName = null, 
    string typeName = null,
    Refresh? refresh = null, 
    CancellationToken cancellationToken = default)
{
    Guard.Argument(document, nameof(document)).NotNull();

    await RemoveNullFieldsFromDocumentAsync(document, document.Id, indexName, typeName, cancellationToken);

    var response = await Client.UpdateAsync<T, object>(
        document.Id, 
        u => u.Doc(document)
            .Index(indexName ?? DocumentMappings.IndexStrategy)
            .Type(typeName ?? DocumentMappings.TypeName)
            .Refresh(refresh), 
        cancellationToken);

    var errorMessage = response.LogResponseIfError(_logger);

    return errorMessage.IsNullOrEmpty() ? Result.Ok() : Result.Fail(errorMessage);
}

public async Task<Result> UpdateAsync(
    string id, 
    object partialDocument, 
    string indexName = null, 
    string typeName = null,
    Refresh? refresh = null, 
    CancellationToken cancellationToken = default)
{
    Guard.Argument(partialDocument, nameof(partialDocument)).NotNull();
    Guard.Argument(id, nameof(id)).NotNull().NotEmpty().NotWhiteSpace();

    await RemoveNullFieldsFromDocumentAsync(partialDocument, id, indexName, typeName, cancellationToken);

    var response = await Client.UpdateAsync<T, object>(
        id, 
        u => u.Doc(partialDocument)
            .Index(indexName ?? DocumentMappings.IndexStrategy)
            .Type(typeName ?? DocumentMappings.TypeName)
            .Refresh(refresh), 
        cancellationToken);

    var errorMessage = response.LogResponseIfError(_logger);

    return errorMessage.IsNullOrEmpty() ? Result.Ok() : Result.Fail(errorMessage);
}

private async Task<Result> RemoveNullFieldsFromDocumentAsync(
    object document,
    string documentId,
    string indexName = null, 
    string typeName = null,
    CancellationToken cancellationToken = default)
{
    var result = Result.Ok();
    var allNullProperties = GetNullPropertyValueNames(document);
    if (allNullProperties.AnyAndNotNull())
    {
        var script = allNullProperties.Select(p => $"ctx._source.remove('{p}')").Aggregate((p1, p2) => $"{p1}; {p2};");
        result = await UpdateByQueryIdAsync(
                                        documentId, 
                                        script,
                                        indexName,
                                        typeName,
                                        cancellationToken: cancellationToken);
    }

    return result;
}

private static IReadOnlyList<string> GetNullPropertyValueNames(object document)
{
    var allPublicProperties =  document.GetType().GetProperties().ToList();

    var allObjects = allPublicProperties.Where(pi => pi.PropertyType.IsClass).ToList();

    var allNames = new List<string>();

    foreach (var propertyInfo in allObjects)
    {
        if (propertyInfo.PropertyType == typeof(string))
        {
            var isNullOrEmpty = ((string) propertyInfo.GetValue(document)).IsNullOrEmpty();
            if (isNullOrEmpty)
            {
                allNames.Add(propertyInfo.Name.ToCamelCase());
            }
        }
        else if (propertyInfo.PropertyType.IsClass)
        {
            if (propertyInfo.GetValue(document).IsNotNull())
            {
                var namesWithobjectName = GetNullPropertyValueNames(propertyInfo.GetValue(document))
                    .Select(p => $"{propertyInfo.PropertyType.Name.ToCamelCase()}.{p.ToCamelCase()}");
                allNames.AddRange(namesWithobjectName);
            }
        }
    }

    return allNames;
}

public async Task<Result> UpdateByQueryIdAsync(
    string documentId,
    string script,
    string indexName = null, 
    string typeName = null, 
    bool waitForCompletion= false,
    CancellationToken cancellationToken = default)
{
    Guard.Argument(documentId, nameof(documentId)).NotNull().NotEmpty().NotWhiteSpace();
    Guard.Argument(script, nameof(script)).NotNull().NotEmpty().NotWhiteSpace();

    var response = await Client.UpdateByQueryAsync<T>(
        u => u.Query(q => q.Ids(i => i.Values(documentId)))
                .Conflicts(Conflicts.Proceed)
                .Script(s => s.Source(script))
                .Refresh()
                .WaitForCompletion(waitForCompletion)
                .Index(indexName ?? DocumentMappings.IndexStrategy)
                .Type(typeName ?? DocumentMappings.TypeName), 
        cancellationToken);

    var errorMessage = response.LogResponseIfError(_logger);

    return errorMessage.IsNullOrEmpty() ? Result.Ok() : Result.Fail(errorMessage);
}

【讨论】:

    猜你喜欢
    • 2012-07-12
    • 2018-08-17
    • 2013-04-01
    • 2015-07-30
    • 1970-01-01
    • 2014-08-31
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多