简体   繁体   English

如何基于父节点在两个随机节点之间创建关系?

[英]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.

相关问题 如何使用节点 ID 在两个现有节点之间创建关系? - How to create relationship between two existing nodes by using node id? 根据节点之间的现有关系创建节点之间的关系 - Create a relationship between nodes based on existing relationship between the nodes 如何创建节点并在另一个新创建的节点上创建一对多关系? - How can I create nodes and create a one-to-many relationship another newly created node? 如何在具有共同属性的两个节点之间创建关系? - How to create a relationship between two nodes with common properties? 如何在两个节点之间的neo4j中创建关系? - How do I create a relationship in neo4j between two nodes? 如何使用C#Neo4jClient在两个节点之间创建关系? - How do I create a relationship between two nodes using C# Neo4jClient? 如何在图数据库中的节点之间创建两个关系 - How to create two relationship between nodes in graph DB 检查Neo4J中2个节点之间是否存在关系,如果没有,则创建具有随机ID的节点 - Check if relationship exits between 2 nodes in Neo4J and if not create node with random id 如何限制 Neo4j 中的两个节点之间只有一种关系? - How can I limit to only one relationship between two nodes in Neo4j? 我将如何查询这两个节点之间的关系? - How would I query for a relationship between these two nodes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM