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

前端 未结 5 794
一生所求
一生所求 2020-12-24 06:56

I am trying to update an existing indexed document. I have indexed tags, title and owners field. Now when the user changes the title, I need to find and update the document

相关标签:
5条回答
  • 2020-12-24 07:35

    A better solution in Nest 7.x:

     await _client.UpdateAsync<ElasticSearchDoc>(doc.Id, u => u.Index("movies").Doc(new ElasticSearchDoc { Title = "Updated title!" }));
    
    0 讨论(0)
  • 2020-12-24 07:46

    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.

    0 讨论(0)
  • 2020-12-24 07:49

    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))
    
    0 讨论(0)
  • 2020-12-24 07:49

    also using Nest 7.x.

    If you want to do only a partial update, you can use this method that worked great for me. You must specify "T, K" where T is the full object and K the partial object. Creating a POCO for every partial update is kind of overwork and annoying. For this issue, you can use anonymous object like so

     public bool PartialUpdate(string id, object entity)
     {
         var result = _elasticClient.Update<T, object>(DocumentPath<T>.Id(id), i => i.Index(_indexName).Doc(entity));
    
         return result.IsValid;
     }
    
    

    Im using the Elastic Common Schema, so here is an example of a partial object for the update:

    new
    {
        Labels = new Dictionary<string, object>
        {
            { "EscalateTo", alert.AlertState == AlertState.Escalation ? escalationId : "" },
            { "EscalateFrom", alert.AlertState == AlertState.Descalation ? escalationId : "" },
        },
        Event = new
        {
            End = alert.WindowEnd,
            Duration = (alert.WindowEnd - storedAlert.StartTime.Value).Ticks
        }
    };
    
    0 讨论(0)
  • 2020-12-24 08:00

    For Nest 2 to update an POCO that already include an ID field:

     var task = client.UpdateAsync<ElasticsearchDocument>(
                        new DocumentPath<ElasticsearchDocument>(doc), u => 
                            u.Index(indexName).Doc(doc));
    
    0 讨论(0)
提交回复
热议问题