如何使用 NEST 更新 ElasticSearch 索引中的现有文档? [英] How do I update an existing document inside ElasticSearch index using NEST?

查看:40
本文介绍了如何使用 NEST 更新 ElasticSearch 索引中的现有文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 inside the index.

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

Should I update and replace the entire document or just the title field?

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<ElasticsearchDocument, IndexedDocument>(
                                  d => d.Index("movies")
                                        .Type(doc.Type)
                                        .Id(doc.Id), doc);
}

这根本行不通.上面的代码会产生一个语法错误.有谁知道使用 ElasticSearch 的 C# NEST 客户端执行此操作的正确方法吗?

It just doesn't work. The code above generates a syntax error. Does anyone know the correct way to do this using the C# NEST client of ElasticSearch?

推荐答案

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

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)
     );

您可以在 来自 GitHub 源的 NEST 更新单元测试.

这篇关于如何使用 NEST 更新 ElasticSearch 索引中的现有文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆