# Enum Indexing As Number Instead Of Text

**URL:** <https://discuss.elastic.co/t/enum-indexing-as-number-instead-of-text/234624>\
**Category:** Elasticsearch\
**Created:** [May 27, 2020, 9:54pm UTC](https://discuss.elastic.co/t/enum-indexing-as-number-instead-of-text/234624 "2020-05-27T21:54:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathaniel\_Jones](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathaniel_jones/32/47048_2.png) [@Nathaniel\_Jones](https://discuss.elastic.co/u/Nathaniel_Jones)\
**Post date:** [May 27, 2020, 9:54pm UTC](https://discuss.elastic.co/t/enum-indexing-as-number-instead-of-text/234624/1 "2020-05-27T21:54:27Z")

</div>

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

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

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

```

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [May 28, 2020, 12:24am UTC](https://discuss.elastic.co/t/enum-indexing-as-number-instead-of-text/234624/2 "2020-05-28T00:24:47Z")

</div>

> [@Nathaniel\_Jones](#):
>
> ```auto
> [JsonConverter(typeof(StringEnumConverter))]
> [Text]
> public Gender Gender { get; set; }
> 
> ```

NEST 7.x does not use [Json.NET](http://Json.NET) so does not know about `JsonConverterAttribute`. With 6.x and 7.x, you can use

```auto
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](https://www.nuget.org/packages/NEST.JsonNetSerializer) can be referenced and [`JsonNetSerializer`](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/custom-serialization.html#_jsonnetserializer) can be hooked up as the serializer to use for your documents

var pool = new SingleNodeConnectionPool(new Uri("[http://localhost:9200](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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 25, 2020, 12:24am UTC](https://discuss.elastic.co/t/enum-indexing-as-number-instead-of-text/234624/3 "2020-06-25T00:24:51Z")

</div>

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