ElasticSearch NEST API将值更新为null [英] ElasticSearch NEST API update value to null
问题描述
我正在使用NEST api,但无法使用client.Update<T, K>
方法将值更新为null
I am using NEST api and I am having trouble using client.Update<T, K>
method to update a value to null
调用update时是否有任何参数或设置可以通过槽式api设置null
?
Is there any parameter or settings when calling update that will allow for null
to be set trough nest api?
我知道我可以做到.
推荐答案
而不是更改整个请求应序列化null的方式,最安全,最隔离的方法是为该属性引入一个单独的POCO进行更新您要清除的具有以下属性.
Rather then changing how null should be serialised for the entire request the safest and most isolated way to do this is to introduce a separate POCO for the update where the property you want to clear has the following attribute.
[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的示例更新:
Example update using that POCO:
var partialUpdate = new PartialElasticsearchProjectWithNull { Country = null };
this._client.Update<ElasticsearchProject, PartialElasticsearchProjectWithNull>(update => update
.Id(3)
.Doc(partialUpdate)
);
这将生成以下JSON:
which will generate the following JSON:
{
"doc": {
"country": null
}
}
这篇关于ElasticSearch NEST API将值更新为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!