【问题标题】:Saving C# enumeration as string instead of int in ElasticSearch 5在 ElasticSearch 5 中将 C# 枚举保存为字符串而不是 int
【发布时间】:2017-05-11 17:49:16
【问题描述】:

我曾经在 Elastic Search 2.0 中枚举类型的类属性上具有 String 属性,以便将枚举值存储为字符串而不是整数:

public enum PaperType
{
    A4 = 0,
    A3 = 1
}

以及在 ElasticSearch 中作为文档存储的类

[ElasticsearchType(Name = "Paper")]
public class Paper
{  
  [String(Store = false, Index = FieldIndexOption.Analyzed)]
  public string Name { get; set; }

  [String(Store = false, Index = FieldIndexOption.Analyzed)]
  public PaperType Type { get; set; }
}

所以 in Type 将被存储为 A3 或 A4 而不是 0 或 1。

在 ElasticSearch 5 中,这些属性发生了变化,

我怎样才能在 ElasticSearch 5 中实现相同的行为或什么属性以及它应该是什么?

谢谢

【问题讨论】:

    标签: elasticsearch enums attributes nest


    【解决方案1】:

    查看文档中的attribute mapping;您可能希望将它们映射为 Keyword 类型。

    如果要将所有enum 保存为字符串,可以将Json.Net StringEnumConverter 添加到JsonSerializerSettings 以用于任何类型的枚举。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 2012-09-05
      • 2018-06-21
      • 2011-05-20
      • 1970-01-01
      • 2021-06-10
      • 2017-07-07
      相关资源
      最近更新 更多