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

前端 未结 5 798
一生所求
一生所求 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: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(DocumentPath.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
        {
            { "EscalateTo", alert.AlertState == AlertState.Escalation ? escalationId : "" },
            { "EscalateFrom", alert.AlertState == AlertState.Descalation ? escalationId : "" },
        },
        Event = new
        {
            End = alert.WindowEnd,
            Duration = (alert.WindowEnd - storedAlert.StartTime.Value).Ticks
        }
    };
    

提交回复
热议问题