Search code examples
c#.netelasticsearch.net-corenest

Nest converts first letter of field to lowercase in query


I am trying to upsert a dictionary<string, string> to elasticsearch but the first letter of the keys converts to small letter. the dictionary property in the entity

public Dictionary<string, Dictionary<string, string>> CurrentJson { get; set; }
public string CURRENT_JSON
{
    set
    {
        CurrentJson = new Dictionary<string, Dictionary<string, string>>
        { { TableName.ToUpper(), JsonConvert.DeserializeObject<Dictionary<string, string>>(value) } };
    }
}

the keys when I set the dictionary will be cap. letters

upsert function

public async Task<T> Upsert(T document, string username = "N/A")
{
    // check if name is duplicated
    if (!await IsUniqueDocument(document))
        throw new Exception("name duplicated");
    
    PrepareDocument(document, username);
    
    IndexRequest<T> request = new IndexRequest<T>(document, IndexName, TypeName, document.Id);
    var response = await Client.IndexAsync<T>(request);
    if (response.IsValid) return document;
    // TODO: check thrown error
    throw new Exception(response.Result.ToString());
}

note: prepareDocument function do not change the dictionary


Solution

  • Have a look at the Field inference documentation of the NEST client: https://www.elastic.co/guide/en/elasticsearch/client/net-api/7.x/field-inference.html#camel-casing

    If you’d like NEST to not change the casing of field names at all, simply pass a Func<string,string> to DefaultFieldNameInferrer that simply returns the input string

    setup = WithConnectionSettings(s => s.DefaultFieldNameInferrer(p => p));