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:
- I tried to add
[JsonProperty(NullValueHandling = NullValueHandling.Include)]
attribute toProductSalesHistory.Disposition
property, but it didn't work. - Adding
() => new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include }
toConnectionSettings
as a parameter is not an option for me as I don't want to get side-effects on another queries.
I solved this problem as follows, due to lack of other ideas:
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.