gpt4 book ai didi

c# - 嵌套版本6.x中的InMemoryConnection Elasticsearch jsonSerializationException

转载 作者:行者123 更新时间:2023-12-02 23:13:27 29 4
gpt4 key购买 nike

使用NEST版本6.x和NEST.JSonSerializer版本6.x,我收到JSONSerialization异常,并使用inMemory elasticsearch进行单元测试。

我尝试使用Nest和JsonSersializer版本7.x,并且运行良好。但是我的生产服务器具有6.x版本,因此我需要在6.x NEST Client上运行测试。

    var response = new
{
took = 1,
timed_out = false,
_shards = new
{
total = 2,
successful = 2,
failed = 0
},
hits = new
{
total = new { value = 25 },
max_score = 1.0,
hits = Enumerable.Range(1, 25).Select(i => (object)new
{
_index = "project",
_type = "project",
_id = $"Project {i}",
_score = 1.0,
_source = new { name = $"Project {i}" }
}).ToArray()
}
};

var responseBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(response));
var connection = new InMemoryConnection(responseBytes, 200);
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(connectionPool,connection).DefaultIndex("project");
settings.DisableDirectStreaming();
var client = new ElasticClient(settings);
var searchResponse = client.Search<Project>(s => s.MatchAll());

预期输出:inMemory elasticsearch的响应
出现错误:
JsonSerializationException:无法将当前JSON对象(例如{“name”:“value”})反序列化为类型'System.Int64',因为该类型需要JSON基本值(例如string,number,boolean,null)才能正确反序列化。

最佳答案

响应主体中总部分的架构在elasticsearch的6.x7.x版本之间进行了更改。如果要使响应正文与NEST 6.x一起使用,则需要从

..
total = new { value = 25 },
..


..
total = 25,
..

完整示例:
var response = new
{
took = 1,
timed_out = false,
_shards = new
{
total = 2,
successful = 2,
failed = 0
},
hits = new
{
total = 25,
max_score = 1.0,
hits = Enumerable.Range(1, 25).Select(i => (object)new
{
_index = "project",
_type = "project",
_id = $"Project {i}",
_score = 1.0,
_source = new { name = $"Project {i}" }
}).ToArray()
}
};

希望能有所帮助。

关于c# - 嵌套版本6.x中的InMemoryConnection Elasticsearch jsonSerializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57946727/

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