Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch NEST API update value to null

I am using NEST api and I am having trouble using client.Update<T, K> method to update a value to 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.

like image 887
Nemanja Malocic Avatar asked Jan 13 '15 09:01

Nemanja Malocic


1 Answers

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

Example 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; }
}

Example update using that POCO:

var partialUpdate = new PartialElasticsearchProjectWithNull { Country = null };

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

which will generate the following JSON:

{
  "doc": {
    "country": null
  }
}
like image 159
Martijn Laarman Avatar answered Sep 22 '22 10:09

Martijn Laarman