7

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.

Vogel612
  • 5,620
  • 5
  • 48
  • 73
Nemanja Malocic
  • 176
  • 1
  • 2
  • 7
  • Have you found a solution for this? This works fine for merge updates but will ignore null value if set. Maybe there is an option for this? – mbudnik Feb 11 '15 at 09:59
  • It seems inserts work the same - ignoring null values. – mbudnik Feb 11 '15 at 10:08

2 Answers2

7

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
  }
}
Martijn Laarman
  • 13,476
  • 44
  • 63
1
  • In my case, I had a .NET DateTime? property, and I wanted to update an ElasticSearch date property to either null or empty.
  • I couldn't do it with the Doc method. It wouldn't push my null assignment to elasticsearch (probably because the elasticsearch date type doesn't support null).

So here's what I did:

  1. I modified the elasticsearch.yml file to include this setting: script.groovy.sandbox.enabled: true.
  2. I modified my C# code to the following to remove the property from the document:

        _elasticClient.Update<MyIndexType, object>(u => u
            .Id(myDocumentId)
            .Index(myIndexName)
            .Type(myType)
            .Script(string.Format("ctx._source.remove(\"{0}\")", myPropertyName))
            .RetryOnConflict(3));
    
Community
  • 1
  • 1
Jim G.
  • 15,141
  • 22
  • 103
  • 166