ElasticSearch NEST API 将值更新为 null [英] ElasticSearch NEST API update value to null

查看:64
本文介绍了ElasticSearch NEST API 将值更新为 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 NEST api,但在使用 client.Update 方法将值更新为 null

I am using NEST api and I am having trouble using client.Update<T, K> method to update a value to null

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

Is there any parameter or settings when calling update that will allow for null to be set trough nest api?

我知道我可以理智地做到这一点.

I know I can do it with sense.

推荐答案

而不是改变整个请求的 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屋!

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