How do I update an existing document inside ElasticSearch index using NEST? How do I update an existing document inside ElasticSearch index using NEST? elasticsearch elasticsearch

How do I update an existing document inside ElasticSearch index using NEST?


I have successfully updated existing items in my Elasticsearch index with NEST using a method like the following. Note in this example, you only need to send a partial document with the fields that you wish to be updated.

    // Create partial document with a dynamic    dynamic updateDoc = new System.Dynamic.ExpandoObject();    updateDoc.Title = "My new title";    var response = client.Update<ElasticsearchDocument, object>(u => u        .Index("movies")        .Id(doc.Id)        .Document(updateDoc)     );

You can find more examples of ways to send updates in the NEST Update Unit Tests from the GitHub Source.


Actually for Nest 2 it's:

dynamic updateFields = new ExpandoObject();updateFields.IsActive = false;updateFields.DateUpdated = DateTime.UtcNow;await _client.UpdateAsync<ElasticSearchDoc, dynamic>(new DocumentPath<ElasticSearchDoc>(id), u => u.Index(indexName).Doc(updateFields))


A better solution in Nest 7.x:

 await _client.UpdateAsync<ElasticSearchDoc>(doc.Id, u => u.Index("movies").Doc(new ElasticSearchDoc { Title = "Updated title!" }));