【问题标题】:Partial update elastic search NEST 2.x部分更新弹性搜索 NEST 2.x
【发布时间】:2016-11-22 13:36:16
【问题描述】:

如何在使用 NEST2 时部分更新记录?

我正在寻找模拟请求: POST /erection/shop/1/_update {"doc": {"new":"0"}}

无需重新创建新记录。不幸的是,我在 www.elastic.co/guide/en/elasticsearch/client/net-api/current/index.html 中没有找到任何有关更新的信息

更新:

var updateResponse = es.Current.Update<MyDocument, MyDocument>   (DocumentPath<MyDocument>.Id(2), descriptor => descriptor
     .Doc(new MyDocument
     {
         name = "new name"
     }));

我运行这段代码,但它完全更新了整个文档。

结果https://gyazo.com/2fdae851bb8bc445f6e8e58abb2f0e3b 我做错了什么?

【问题讨论】:

  • 你会发现一个例子here。
  • 感谢答案,但我不明白哪个命名空间有 DocumentPartial 和 DocumentPath。我尝试使用您的代码,但出现错误。 var updateResponse = es.Current.Update&lt;Document, MyDocument&gt;(descriptor =&gt; descriptor .Id(1) .Doc(new MyDocument { name = "qwerty" }));我尝试使用此代码,但它更新了所有文档,并将除“名称”之外的所有字段留空
  • 您可以发布您的完整示例吗?

标签: c# elasticsearch updates nest


【解决方案1】:

使用匿名对象或其他具有您想要更新的属性的类。试试这个代码:

var updateResponse = es.Current.Update<MyDocument, object>(1, descriptor => descriptor
            .Doc(new { name = "new name" }));

【讨论】:

  • 非常感谢,Mohammad 这正是我所需要的
  • 如果其他用户的回答真实,请勾选回答
  • +1 表示匿名类型。我可能会使用与正在更新的类型相同的属性名称大小写 - json 中的序列化大小写将与正在使用的任何序列化程序设置匹配。
  • @Russ Cam:不明白你的问题!请进一步说明或举例
  • @MohammadAkbari - 我的意思是,使用 new { Name = "new name" } 作为匿名类型而不是 new { name = "new name" } 并让连接设置上的 .DefaultFieldNameInferrer() 处理属性名称的大小写
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-10
  • 2020-09-04
  • 1970-01-01
  • 2019-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多