索引时 (ElasticClient.IndexMany()) 抛出 StackOverflowException

While Indexing (ElasticClient.IndexMany()) throws StackOverflowException

我以前是这样从数据库中获取文档的:

private List<Student> GetStudents()
        {
                using (Context context = new Context(connectionString))
                {
                    return context.Students
                        .ToList<Student>(); 
                }
        }

然后我用 IndexMany() 索引了一些文档,一切都很好。

但是当我像这样在我的文档中包含“用户”时:

private List<Student> GetStudents()
        {
                using (Context context = new Context(connectionString))
                {
                    return context.Students
                        .Include(s => s.User)
                        .ToList<Student>(); 
                }
        }

程序运行到“client.IndexMany(students,studentsIndexName);”而不是抛出 未知模块

中的“WhosebugException”
public int InitializeStudents()
        {
            string studentsIndexName = "students";
            client.Indices.Create(GetStudentMap(studentsIndexName));
            List<Student> students = GetStudents();

            client.IndexMany(students,studentsIndexName);

            return students.Count;
        }

我收集了大约 1000 份文件,并且 起初试图只索引其中的一部分(5)但我遇到了同样的问题 然后我认为问题出现是因为索引更新太频繁所以我在索引时禁用了更新, 但它也没有帮助

public int InitializeStudents()
    

        {
                string studentsIndexName = "students";
                client.Indices.Create(GetStudentMap(studentsIndexName).Settings(s => s
                .RefreshInterval(-1)
                ));
                List<Student> students = GetStudents();
    
                client.IndexMany(students.Take<Student>(5),studentsIndexName);
    
                client.Indices.UpdateSettings(studentsIndexName, s => s.IndexSettings(o => o.RefreshInterval(1)));
                return students.Count;
            }
    
    
    private static CreateIndexDescriptor GetStudentMap(string indexName)
            {
                CreateIndexDescriptor map = new CreateIndexDescriptor(indexName);
                map.Mappings(M => M
                    .Map<Student>(m => m
                        .Properties(prop => prop
                            .Text(s => s
                                .Name(n => n.FullName)
                            )
                            
                            .Object<User>(o => o
                                .Name(s => s.User)
                                
    
                             )
                            .Number(s => s
                               .Name(n => n.Id)
                               .Type(NumberType.Integer)
                            )
                        )
                     )
                  )
               ;
                return map;
    
            }

我使用 Elasticsearch.Net 7.10 和 EntityFramework 5.0。 那么你能告诉我哪里出了问题或者我应该尝试什么吗?

正如评论中所建议的,听起来 StudentUser 类型中可能存在循环引用。 NEST 7.x 使用的 JSON 序列化程序不处理循环引用,这会导致堆栈溢出。

我强烈建议为将索引到 Elasticsearch 的文档定义简单的 POCO,并将域类型映射到这些。

如果您真的想要索引您拥有的类型,那么您可以使用 JsonNetSerializer to do so, which can be configured to handle circular references.