Search code examples
azureazure-cognitive-searchazure-search-.net-sdk

Azure Search SDK null field not set on Merge action


I'm using Microsoft.Azure.Search version 3.0.1,

I'm trying the following:

// subset of my index's fields
private class SyncFields
{   
    public string Id { get; set; }
    public DateTimeOffset? ApprovedOn { get; set; }
    public DateTimeOffset? IgnoredOn { get; set; }
}

public void Sync()
{
    var sync = new SyncFields
    {
        Id = "94303",
        ApprovedOn = null,
        IgnoredOn = DateTime.UtcNow
    };

    var searchClient = new SearchServiceClient("xxxx",
        new SearchCredentials("xxxx"));
    searchClient.SerializationSettings.NullValueHandling = NullValueHandling.Include;

    using (var client = searchClient.Indexes.GetClient("xxxx"))
    {
        client.SerializationSettings.NullValueHandling = NullValueHandling.Include;
        var batch = IndexBatch.Merge<SyncFields>(new[] { sync });
        client.Documents.Index<SyncFields>(batch);
    }
}

This isn't settings ApprovedOn to null. It ignores it. If I set a non-null value, it does set it.

According to the documentation here the merge operation updates the field to be null. And in fact, if I make this Http post request manually with JSON, this is true. But the SDK isn't updating the field(s) to null. What am I missing?


Solution

  • This is a known limitation of the typed overloads of the Index family of methods. The issue is described in detail here: https://github.com/Azure/azure-sdk-for-net/issues/1804

    Some workarounds:

    1. Use the untyped version of Index instead for merge scenarios.
    2. Use Upload instead of Merge.
    3. Put [JsonProperty(NullValueHandling = NullValueHandling.Include)] on the properties of your model class that you need to explicitly set to null in a merge operation (not recommended if you have many fields in your index).
    4. Implement a custom converter.