嵌套 6.2.0 自引用循环



我正在使用 Nest v6.2.0 连接到弹性搜索。

当我尝试将 A 类型的文档添加到索引时,我收到自引用循环错误,因为类型 A 的对象具有类型 B 的属性,而类型 B 具有类型 A 的属性。A和B类型的两个对象都是数据库对象。

我可以在 JsonParser 上将 ReferenceLoopHandling 设置设置为 ReferenceLoopHandling.Ignore ,它可以很好地解析对象,但我似乎无法将该 json 字符串发送到弹性搜索。那么如何在 Nest 使用的 Json 解析器上设置相同的设置呢?因此,我可以将对象发送到弹性搜索,并且 Nest 可以正确解析对象而不会出现自引用循环错误。

您可能希望使用不同的、更简单的 POCO 来对发送到 Elasticsearch 的文档进行建模,以避免通过自引用循环出现潜在的大型对象图。

可以引用 nuget 包Nest.JsonNetSerializer并挂接 Json.NET 作为序列化程序以与 NEST 一起使用,从而配置 ReferenceLoopHandling 属性

var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool, (builtInSerializer, connectionSettings) =>
    new JsonNetSerializer(builtInSerializer, connectionSettings, () => new JsonSerializerSettings
    {
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
    }));
var client = new ElasticClient(settings);

最新更新