I have the below C# class.
public class ElasticSearchDocument
{
public string Id { get; set; } = Guid.NewGuid().ToString();
public string Description { get; set; }
}
I am also using templates for my documents, the one below is for the demonstrated test.
{
"version": 2,
"index_patterns": "documents-test*",
"order": 2,
"aliases": {
"docs-test": {}
},
"settings": {
"number_of_shards": 1
},
"mappings": {
"_doc": {
"dynamic": "strict",
"properties": {
"id": {
"type": "keyword"
},
"description": {
"enabled": false
}
}
}
}
}
I am setting the Description
property to a has value
and index it. Below an example in the database.
{
"_index": "documents-test-2019-07-2-2",
"_type": "_doc",
"_id": "55096ff7-5072-4ded-b6a3-94b8e155c9d0",
"_score": 1,
"_source": {
"id": "55096ff7-5072-4ded-b6a3-94b8e155c9d0",
"description": "has value"
}
}
Querying the document, setting the Description
property to null
and using the below NEST IElasticClient.UpdateAsync
method updating a document.
public async Task<Result> UpdateAsync(
T document,
string indexName = null,
string typeName = null,
Refresh ? refresh = null,
CancellationToken cancellationToken =
default) {
var response = await Client.UpdateAsync<T,
object>(
document.Id,
u => u.Doc(document)
.Index(indexName ? ? DocumentMappings.IndexStrategy)
.Type(typeName ? ? DocumentMappings.TypeName)
.Refresh(refresh),
cancellationToken);
var errorMessage = response.LogResponseIfError(_logger);
return errorMessage.IsNullOrEmpty() ? Result.Ok() : Result.Fail(errorMessage);
}
The problem is after the updating command the document is unchanged with the description
field to have the value has value
.
The most appropriate solution in my opinion would be somehow when setting the C# class Description
property to null and updating Elastic Search the field to be removed from the document.
I have seen couple of answers but not sure what might have changed or if there is a better solution than update_by_query or overriding behavior of ignoring null with property attribute (cumbersome) with just using NEST