question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MapFromAttributes not working with Enum fields

See original GitHub issue

The MapFromAttributes is not working when the class includes an enum. Here is a simple example:

    public void InitializeES()
    {
        //drop the index for debugging purposes, remove this later
        var deleteResponse = _client.DeleteIndex(f => f.Index(_client.Infer.DefaultIndex));
        VerifyResponse(deleteResponse);
        if (!_client.IndexExists(i => i.Index(_client.Infer.DefaultIndex)).Exists)
        {
            var response = _client.CreateIndex(_client.Infer.DefaultIndex);
            VerifyResponse(response);
        }
        VerifyResponse(_client.Map<TestClass>(m => m.MapFromAttributes()));
    }

    private static void VerifyResponse(IResponse response)
    {
        if (!response.IsValid)
        {
            throw new ElasticsearchServerException(response.ServerError);
        }
    }

    [ElasticType(Name = "testclass")]
    public class TestClass
    {
        public int Id { get; set; }

        [ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
        [JsonConverter(typeof(StringEnumConverter))]
        public EnumTester EnumTest { get; set; }
    }

    public enum EnumTester
    {
        Value1,
        Value2
    }

This is the resulting structure from calling GET testindex/_mapping:

{ “testindex”: { “mappings”: { “testclass”: { “properties”: { “id”: { “type”: “integer” } } } } } }

The enum is missing from the mapped data structure.

I’m on ES 1.3.0, NEST 1.0.2

Am I doing something wrong or is this a bug?

Thanks in advance guys, I love ES and I love NEST!

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
bloritschcommented, Mar 26, 2015

Weighing in here, as the guy who was blindsided by this issue and asked the question.

We are talking about documents, not POCO objects. How the runtime stores and works with enums is irrelevant from a document standpoint. What’s important is what those numbers convey, and that meaning is expressed in words. I would argue for storing the enum as a string, the way that ElasticSearch 1.4.x does now.

I would go a step further, and when you automap based on types, all enums would be treated as strings that are not analyzed. The number of values an enum can hold is finite, and they should never be tokenized and split apart (i.e. with underscores, etc.). Feel free to prove me wrong, but I would argue enum types would be most often used with terms filters.

1reaction
gmarzcommented, Mar 26, 2015

We can either default to integer, string, or throw an exception. I’m torn- defaulting to integer seems technically correct, since that’s how enums are treated naturally. On the other hand, I think most people are expecting/want a string. Thoughts @Mpdreamz ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enum.values() not working in GWT throws compilation error
1 Answer. Got it fixed by adding the Enum class package under source path="" in gwt. xml.
Read more >
Is public final completely (not) ok in constant enum fields?
My personal opinion is that public final is generally fine everywhere, not just on enums. In my book, there are only two reasons...
Read more >
Attaching Values to Java Enum
In this tutorial, we'll use the enum features as a Java class to attach the values we want.
Read more >
Getting and Setting Fields with Enum Types
This reflection Java tutorial describes using reflection for accessing and manipulating classes, fields, methods, and constructors.
Read more >
Enum in Java
Enum was introduced in Java 1.5 as a new type whose fields consists of a fixed set of constants. For example, we can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found