gpt4 book ai didi

elasticsearch - 在ElasticSearch中序列化RegionInfo

转载 作者:行者123 更新时间:2023-12-02 23:16:33 26 4
gpt4 key购买 nike

看来RegionInfo对象在序列化方面已经被遗忘了。 CultureInfo很好用,并且可以在字符串之间进行序列化。尝试抛出RegionInfo对象时,我发现RegionInfo的所有属性都无法进行反序列化,因为没有构造函数将所有这些属性取反。我只想将RegionInfo序列化和反序列化为字符串,例如CultureInfo,但是还不太清楚。

我的尝试:

我创建了一个regioninfo转换器

public class RegionInfoConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, ((RegionInfo)value).Name);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var token = JToken.Load(reader);
return new RegionInfo(token.ToObject<string>());
}

public override bool CanConvert(Type objectType)
{
return typeof(RegionInfo) == objectType;
}
}

我把它塞进ConnectionSettings中:
var connectionSettings = new ConnectionSettings(pool,
(builtin, settings) => new JsonNetSerializer(
builtin,
settings,
contractJsonConverters: new JsonConverter[] { new RegionInfoConverter() })
);

但我得到了错误: object mapping for [region] tried to parse field [region] as object, but found a concrete value
听起来好像我的序列化器片段之一是错误的,但我觉得我不太了解这是哪一部分。谢谢。

最佳答案

我认为这里的问题可能是Elasticsearch最初从要索引的文档中推断出objectRegionInfo数据类型映射,现在正在传递stringRegionInfo值。您可能需要删除索引并重新创建,将RegionInfo属性映射为keyword数据类型。

这是一个有效的例子

private static void Main()
{
var defaultIndex = "my_index";
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

var settings = new ConnectionSettings(pool, (b, s) =>
new JsonNetSerializer(b, s, contractJsonConverters: new JsonConverter[] { new RegionInfoConverter() })
)
.DefaultIndex(defaultIndex);

var client = new ElasticClient(settings);

if (client.IndexExists(defaultIndex).Exists)
client.DeleteIndex(defaultIndex);

var createIndexResponse = client.CreateIndex(defaultIndex, c => c
.Settings(s => s
.NumberOfShards(1)
.NumberOfReplicas(0)
)
.Mappings(m => m
.Map<MyEntity>(mm => mm
.AutoMap()
.Properties(p => p
.Keyword(k => k
.Name(n => n.RegionInfo)
)
)
)
)
);

var indexResponse = client.Index(new MyEntity
{
RegionInfo = RegionInfo.CurrentRegion
}, i => i.Refresh(Refresh.WaitFor));
}

public class MyEntity
{
public RegionInfo RegionInfo { get; set; }
}

public class RegionInfoConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value == null)
{
writer.WriteNull();
return;
}

writer.WriteValue(((RegionInfo)value).Name);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;

if (reader.TokenType != JsonToken.String)
throw new JsonSerializationException($"Cannot deserialize {nameof(RegionInfo)} from {reader.TokenType}");

return new RegionInfo((string)reader.Value);
}

public override bool CanConvert(Type objectType)
{
return typeof(RegionInfo) == objectType;
}
}

索引请求发送以下JSON
{
"regionInfo": "AU"
}

关于elasticsearch - 在ElasticSearch中序列化RegionInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54100013/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com