Elasticsearch/NEST 6-将枚举存储为字符串 [英] Elasticsearch / NEST 6 - storing enums as string
本文介绍了Elasticsearch/NEST 6-将枚举存储为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在NEST6中将枚举存储为字符串?
Is it possible to store enums as string in NEST6?
我已经尝试过了,但是似乎没有用.有什么建议吗?
I've tried this but it does not seem to work. Any suggestions?
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()
}
});
}
推荐答案
如果要为所有枚举设置它,可以使用
If you'd like to set it for all enums, you can do so with
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"
}
我认为您尝试设置转换器的方式也应该起作用,而这是一个错误,实际上并非如此.我将解决一个问题.
I think the way that you're attempting to set converters should also work and is a bug that it doesn't. I'll open an issue to address.
这篇关于Elasticsearch/NEST 6-将枚举存储为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文