How to update value to null using Elasticsearch NEST APIs UpdateAsync method?

1.2k views Asked by At

I'm using Elasticsearch NEST API (7.8.1) and I'm having trouble with using client.UpdateAsync<T> method to update a value to null.

Is there any workaround to solve this issue?

Example model:

public class ProductSalesHistory
{
    public Id { get; set; }
    public Sku { get; set; }
    public Disposition { get; set; } //This should be null after update
}

Example of the original document:

{
    "id": 1, 
    "sku": "somesku", 
    "disposition": "C" 
}

Example of updated document:

{
    "id": 1, 
    "sku": "somesku", 
    "disposition": null 
}

Example of NEST API call:

var response = await Client.UpdateAsync<ProductSalesHistory>(id, u => u
    .Index(IndexName)
    .Doc(document)
    .DocAsUpsert(true)
    .Refresh(Refresh.False));

As a result Elasticsearch NEST serializes updated document before sending it to API to such JSON:

{
    "id": 1, 
    "sku": "somesku"
}

As you can see no "disposition" value was provided to Elasticsearch and as the result, nothing is changed in the document.

What I tried:

  1. I tried to add [JsonProperty(NullValueHandling = NullValueHandling.Include)] attribute to ProductSalesHistory.Disposition property, but it didn't work.
  2. Adding () => new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include } to ConnectionSettings as a parameter is not an option for me as I don't want to get side-effects on another queries.
1

There are 1 answers

0
Maksim On BEST ANSWER

I solved this problem as follows, due to lack of other ideas:

var response = await Client.IndexAsync(document, i => i
    .Index(IndexName);

IndexAsync allows to update values to null and also supports update of existing documents and creation of new documents. As we do a lot of such operations I can say that performance of such updates is pretty much the same.