I used to get my documents from Database like that:
private List<Student> GetStudents()
{
using (Context context = new Context(connectionString))
{
return context.Students
.ToList<Student>();
}
}
And then I indexed some documents with IndexMany() and everything was good.
But when I Include "User": to my Documents like that:
private List<Student> GetStudents()
{
using (Context context = new Context(connectionString))
{
return context.Students
.Include(s => s.User)
.ToList<Student>();
}
}
Program works until "client.IndexMany(students,studentsIndexName);" and than throws "StackOverFlowException" in unknown Module
public int InitializeStudents()
{
string studentsIndexName = "students";
client.Indices.Create(GetStudentMap(studentsIndexName));
List<Student> students = GetStudents();
client.IndexMany(students,studentsIndexName);
return students.Count;
}
I have about 1000 documents in collection and at first a tryed to index only part of them (5) but i had the same problem Then I thought that problem appears beсause index upadates too often so i disabled upadating while indexing, but it also didn't help
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;
}
I use Elasticsearch.Net 7.10 and EntityFramework 5.0. So can you give some ideas what is wrong or what should I try, please?
As suggested in the comments, it sounds like there may be a circular reference in the Student
and User
types. The JSON serializer used by the NEST 7.x does not handle circular references, which would lead to a stack overflow.
I would strongly recommend defining simple POCOs for the documents that will be indexed into Elasticsearch, and mapping the domain types to these.
If you really want to index the types you have, then you may be able to use the JsonNetSerializer
to do so, which can be configured to handle circular references.