Elasticsearch With NEST Using C# nameof Not Working

Written by Ken Dale

Imagine you’re using Elasticsearch with your strongly typed C# models and nameof(MyProperty) and wondering why it doesn’t work. Turns out, Elasticsearch with NEST by default translates MyProperty to myProperty (initial lowercase, as one would expect from JSON conventionally).

This behavior can be adjusted though if you’d prefer to not transform the property name, which also enables nameof(MyProperty) to be used.

See the following examples, they both work. Note the difference in character case and whether DefaultFieldNameInferrer(x => x) is applied:

var elasticClient = new ElasticClient(
    new ConnectionSettings(elasticSearch.Url)
);

var results = await elasticClient.SearchAsync<Person>(s => s
    .Index(TestIndex)
    .Query(
        y => y.MultiMatch(
            mm => mm.Query("test")
                    .Fields(f => f.Field("givenName")))));

Setting DefaultFieldNameInferrer(x => x) allows use of nameof:

var elasticClient = new ElasticClient(
    new ConnectionSettings(elasticSearch.Url)
        // https://github.com/elastic/elasticsearch-net/issues/1528#issuecomment-134221775
        .DefaultFieldNameInferrer(x => x)
);

var results = await elasticClient.SearchAsync<Person>(s => s
    .Index(TestIndex)
    .Query(
        y => y.MultiMatch(
            mm => mm.Query("test")
                    .Fields(f => f.Field(nameof(Person.GivenName))))));

Full code is available here: https://github.com/kendaleiv/ElasticsearchPropertiesCase -> https://github.com/kendaleiv/ElasticsearchPropertiesCase/blob/master/Tests.cs.

Hope this helps in some way!

Published April 20, 2020 by

undefined avatar
Ken Dale Github Senior Application Developer (Former)

Suggested Reading