【问题标题】:Delete Nested document in Elastic search using NEST C#使用 NEST C# 在弹性搜索中删除嵌套文档
【发布时间】:2015-05-05 04:38:18
【问题描述】:

我们如何在使用 Nest 库的弹性搜索中只删除嵌套对象而不是索引。

public class Make
{
   public string MakeId {get;set;}
   public string MakeName {get;set;}
   public string Address { get;set;}

   [ElasticProperty(Type = FieldType.Nested)]
   public List<Cars> Models {get;set;}
}

在上面的映射中,我想删除一个模型条目而不删除整个索引。

我尝试使用 DeleteByQuery 进行删除,但它会删除整个 Make 索引。

【问题讨论】:

    标签: c# elasticsearch nest


    【解决方案1】:

    如果你不介意脚本,你可以试试:

    var updateResponse = client.Update<Make>(descriptor => descriptor
        .Id(documentId)
        .Script("ctx._source.models.remove(0)")
        .Lang("groovy"));
    

    或者没有脚本

    var make = new Make {Id = "1", Models = new List<Cars>
    {
        new Cars{Name = "test"},
        new Cars{Name = "test2"}
    }};
    
    make.Models.RemoveAt(1);
    
    var updateResponse = client.Update<Make>(descriptor => descriptor
        .Id("1")
        .Doc(make));
    

    【讨论】:

    • 感谢罗布的建议。这意味着删除始终适用于整个文档,我们绝不可以只删除嵌套对象。所以我们只需要对嵌套文档进行更新。
    猜你喜欢
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    相关资源
    最近更新 更多