如何使用NEST更新ElasticSearch索引中的现有文档?

我正在尝试更新现有的索引文档。 我有索引标签,标题和所有者字段。 现在,当用户更改标题时,我需要查找并更新索引中的文档。

我应该更新和替换整个文档还是只更改标题字段?

public void UpdateDoc(ElasticsearchDocument doc) { Uri localhost = new Uri("http://localhost:9200"); var setting = new ConnectionSettings(localhost); setting.SetDefaultIndex("movies"); var client = new ElasticClient(setting); IUpdateResponse resp = client.Update( d => d.Index("movies") .Type(doc.Type) .Id(doc.Id), doc); } 

它只是不起作用。 上面的代码生成语法错误。 有没有人知道使用ElasticSearch的C#NEST客户端执行此操作的正确方法?

我已使用如下方法使用NEST成功更新了我的Elasticsearch索引中的现有项目。 请注意,在此示例中,您只需发送包含要更新的字段的部分文档。

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

您可以在GitHub源中找到更多在NEST更新unit testing中发送更新的方法示例。

实际上对于Nest 2来说:

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

对于Nest 2,更新已包含ID字段的POCO:

  var task = client.UpdateAsync( new DocumentPath(doc), u => u.Index(indexName).Doc(doc));