Joshua I
Joshua I

Reputation: 560

Delete Nested document in Elastic search using NEST C#

How do we delete only nested objects and not the index in elastic search using Nest library.

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

In the above mapping i want to delete one entry of Models without deleting whole index.

I tried deleting using DeleteByQuery but it deletes the entire Make index.

Upvotes: 0

Views: 1235

Answers (1)

Rob
Rob

Reputation: 9979

If you don't mind scripts, you can try:

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

or without script

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

Upvotes: 1

Related Questions