Search code examples
c#elasticsearchnest

Nest 6.2.0 Self Referencing Loop


I am using Nest v6.2.0 to connect to elastic search.

When I am trying to add a document of type A to an index I get a Self Referencing loop error because the object of type A has a property of type B and type B has a property of type A. Both objects of type A & B are database objects.

I can set the ReferenceLoopHandling setting to ReferenceLoopHandling.Ignore on a JsonParser and it can parse the object fine, but I can't seem to send that json string to elastic search. So how can I set the same setting on the Json Parser used by Nest? SO that I can send the object to elastic search and Nest can correctly parse the object without the self referencing loop error.


Solution

  • You may want to use different, simpler POCOs to model the documents that you're sending to Elasticsearch, to avoid potentially large object graphs through self-referencing loops.

    You can reference Nest.JsonNetSerializer nuget package and hook up Json.NET as the serializer to use with NEST, configuring the ReferenceLoopHandling property

    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);