bre*_*vhb 5 elasticsearch nest
是否可以在 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()
}
});
}
Run Code Online (Sandbox Code Playgroud)
使用属性上的StringEnumAttribute属性。这向内部序列化器发出信号,将枚举序列化为字符串。在使用它时,您不需要使用NEST.JsonNetSerializer 包
如果你想为所有枚举设置它,你可以这样做
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
}
Run Code Online (Sandbox Code Playgroud)
这会产生一个类似的请求
POST http://localhost:9200/examples/product
{
"foo": "Bar"
}
Run Code Online (Sandbox Code Playgroud)
我认为您尝试设置转换器的方式也应该有效,并且是一个错误,但事实并非如此。我将打开一个问题来解决。
| 归档时间: |
|
| 查看次数: |
2520 次 |
| 最近记录: |