Search code examples
elasticsearchnest

Elastic NEST De-serializing the wrong field


Using ElasticSearch.Net v6.0.2

Given the Indexed item

   {
     "PurchaseFrequency": 76,
     "purchaseFrequency": 80 
   }

and the POCO Object

public class Product
    {
      public int PurchaseFrequency { get; set; }
    }

and the setting

this.DefaultFieldNameInferrer(x => x);

Nest is returning a PurchaseFrequency = 80 even though this is the wrong field.

How can I get NEST to pull the correct cased field from ElasticSearch?


Solution

  • I don't think that this is going to be easily possible because this behaviour is defined in Json.NET, which NEST uses internally (not a direct dependency in 6.x, it's IL-merged into the assembly).

    For example,

    JsonConvert.DeserializeAnonymousType("{\"a\":1, \"A\":2}", new { a = 0 })
    

    deserializes the anonymous type property a value to 2. But

    JsonConvert.DeserializeAnonymousType("{\"A\":2, \"a\":1}", new { a = 0 })
    

    deserializes the anonymous type property a value to 1 i.e. the order of properties as they appear in the returned JSON has a bearing on the final value assigned to a property on an instance of a type.

    If you can, avoid JSON property names that differ only in case. If you can't, then you'd need to hook up the JsonNetSerializer in the NEST.JsonSerializer nuget package and write a custom JsonConverter for your type which only honours the exact casing expected.