Enum Indexing As Number Instead Of Text

We just upgraded from Elasticsearch/Nest 5.1.1 to 7.6.2 and are seeing a difference in how C# enums are being indexed. Previously, the JsonConverter attribute would store the enum in the index as a text string that we would query against. Now it is storing it as a number and queries are failing to return anything when searching by the enum. Below is how the enum property is set with attributes and the enum definition itself

[JsonConverter(typeof(StringEnumConverter))]
[Text]
public Gender Gender { get; set; }

[Serializable]
public enum Gender
{
    Unknown = 'U',
    Male = 'M',
    Female = 'F'
}

NEST 7.x does not use Json.NET so does not know about JsonConverterAttribute. With 6.x and 7.x, you can use

using System.Runtime.Serialization;
using Elasticsearch.Net;

[Serializable]
[StringEnum]
public enum Gender
{
    [EnumMember(Value = "U")]
    Unknown = 'U',
    [EnumMember(Value = "M")]
    Male = 'M',
    [EnumMember(Value = "F")]
    Female = 'F'
}

Alternatively, the Nest.JsonNetSerializer nuget package can be referenced and JsonNetSerializer can be hooked up as the serializer to use for your documents

var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings =
new ConnectionSettings(pool, sourceSerializer: JsonNetSerializer.Default);
var client = new ElasticClient(connectionSettings);

This should continue to work with JsonConverterAttribute as before.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.