gpt4 book ai didi

c# - ElasticSearch NEST API 将值更新为空

转载 作者:太空狗 更新时间:2023-10-29 20:16:42 25 4
gpt4 key购买 nike

我正在使用 NEST api,但在使用 client.Update<T, K> 时遇到问题将值更新为 null 的方法

调用更新时是否有任何参数或设置允许 null要通过 nest api 设置吗?

我知道我可以凭感觉去做。

最佳答案

而不是改变 null 应该如何为整个请求序列化,最安全和最独立的方法是为更新引入一个单独的 POCO,其中您要清除的属性具有以下属性。

[JsonProperty(NullValueHandling = NullValueHandling.Include)]

示例 POCO:

// <summary>
/// This POCO models an ElasticsearchProject that allows country to serialize to null explicitly
/// So that we can use it to clear contents in the Update API
/// </summary>
public class PartialElasticsearchProjectWithNull
{
[JsonProperty(NullValueHandling = NullValueHandling.Include)]
public string Country { get; set; }
}

使用该 POCO 的示例更新:

var partialUpdate = new PartialElasticsearchProjectWithNull { Country = null };

this._client.Update<ElasticsearchProject, PartialElasticsearchProjectWithNull>(update => update
.Id(3)
.Doc(partialUpdate)
);

这将生成以下 JSON:

{
"doc": {
"country": null
}
}

关于c# - ElasticSearch NEST API 将值更新为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27918354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com