I am working on a PoC for my company in an effort to move towards a graph database to leverage some relationship analysis benefits from using graph databases.
I've created a dataset as follows:
foreach (x in range(1,1400) | create (:District {name: "District " + x, id: "district-" + x}))
foreach (x in range(1,10000) | create (:School {name: "School " + x, id: "school-" + x}))
foreach (x in range(1,50000) | create (:Teacher {name: "Teacher " + x, id: "teacher-" + x}))
foreach (x in range(1,50000) | create (:Class {name: "Class " + x, id: "class-" + x}))
I've started creating the basic relationships with these queries:
Create School -> District
match (s: School)
match (d: District)
with collect (distinct s) as school, collect (distinct d) as districts
foreach (school in schools |
foreach (district in [districts[toInteger(rand()*size(districts))]] |
create unique (school)-[:belongs_to]->(district)
)
)
Create Class -> School
match (c:Class)
match (s:School)
with collect (distinct c) as classes, collect (distinct s) as schools
foreach (class in classes |
foreach (school in [schools[toInteger(rand()*size(schools))]] |
create unique (class)<-[:has]-(school)
)
)
Create Teacher -> School
match (t:Teacher)
match (s:School)
with collect (distinct t) as teachers, collect (distinct s) as schools
foreach (teacher in teachers |
foreach (school in [schools[toInteger(rand()*size(schools))]] |
create unique (teacher)-[:teachers_at]->(school)
)
)
The problem I am currently facing is this: I need to take each Teacher and assign it to 0-2 classes at random, but only for classes that belong to the school that that teacher teaches at. I've tried putting MATCH
statements inside of the FOREACH
blocks, but that is against the rules for Cypher queries.
I am looking for suggestions on creating this specific query.
Note: As an aside, I am going to have to do this process again for a group of 250,000 students (randomly assign to a school, then randomly assign to 0-4 classes).
You can select a pattern from the teachers to the schools. Then from school to the classes. And then randomly sort classes, and then use the limit:
MATCH (teacher:Teacher) WITH teacher
MATCH (teacher)-[:teachers_at]->(:School)-[:has]->(class:Class)
WITH teacher,
class ORDER BY RAND()
WITH teacher,
collect(class)[0..toInteger(rand()*3)] as classes
UNWIND classes as class
CREATE (teacher)-[:has_class]->(class)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.