[英]How can I create a relationship between two random nodes based on parent node?
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. 我正在为我的公司开发PoC,以努力转向图形数据库,以利用使用图形数据库带来的一些关系分析收益。
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. 我当前面临的问题是:我需要带每个老师并将其随机分配给0-2个班级,但仅适用于该老师所教学校的班级。 I've tried putting
MATCH
statements inside of the FOREACH
blocks, but that is against the rules for Cypher queries. 我尝试过将
MATCH
语句放在FOREACH
块中,但这违反了Cypher查询的规则。
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).
注意:顺便说一句,我将不得不为一组250,000名学生(随机分配到学校,然后随机分配到0-4个班级)再次执行此过程。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.