是否可以在NEST6中将枚举存储为字符串?
我已经尝试了这个,但似乎没有用。有什么建议吗?
var pool = new SingleNodeConnectionPool(new Uri(context.ConnectionString));
connectionSettings = new ConnectionSettings(pool, connection, SourceSerializer());
private static ConnectionSettings.SourceSerializerFactory SourceSerializer()
{
return (builtin, settings) => new JsonNetSerializer(builtin, settings,
() => new JsonSerializerSettings
{
Converters = new List<JsonConverter>
{
new StringEnumConverter()
}
});
}
答案 0 :(得分:2)
使用酒店的StringEnumAttribute
attribute。这向内部串行器发出信号,将枚举序列化为字符串。使用此功能时,您无需使用NEST.JsonNetSerializer package
如果您想为所有枚举设置它,可以使用
进行设置private static void Main()
{
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings = new ConnectionSettings(
pool,
(builtin, settings) => new JsonNetSerializer(builtin, settings,
contractJsonConverters: new JsonConverter[] { new StringEnumConverter() }));
var client = new ElasticClient(connectionSettings);
client.Index(new Product { Foo = Foo.Bar }, i => i.Index("examples"));
}
public class Product
{
public Foo Foo { get;set; }
}
public enum Foo
{
Bar
}
产生类似
的请求POST http://localhost:9200/examples/product
{
"foo": "Bar"
}
我认为你试图设置转换器的方式也应该有效,而且它不是一个错误。我将打开一个问题来解决。