0

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.

1 Answer 1

1

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));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the suggestion Rob. That means delete is always meant for entire document and in no way we can delete only nested object. So we need to do only update for nested document.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.