cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Connect one node to multiple nodes

Hi Everyone,

I have a problem with connecting duplicate values together and create a Master/Child relationship. I was able to solve the issue when there is only one duplicate node for each original (master) record. When there are 2 or more duplicates, things get messy.

To make things simple, I have queried the following nodes:

3X_9_5_95835ba66cae6ba6aefd03916e33752bb9ddc77d.png

I want Three OUT [MASTER_OF] relation to go from one node to the three other nodes

What I did until now is to identify the master node and I did it using the following:

MATCH (i:INTID{name:"575"})
MATCH (n:PERSON)-[]->(i)
WITH n ORDER BY n.overall DESC limit 1
RETURN n

3X_5_d_5d6e549cc8f83856d67aa10f07e6ec8b12ec7c04.png

Identify the children records:

MATCH (i:INTID{name:"575"})
MATCH (n:PERSON)-[]->(i)
WITH n ORDER BY n.overall DESC SKIP 1
RETURN n

3X_2_b_2b9963dea438c2ddb85348749d9ad6e5ae6fe3e3.png

But then I can't proceed from here.

I was thinking of trying to combining both queries in certain way. Using FOREACH on the child records to iterate through the nodes and create a MASTER_OF relationship between master node and children node.

1 REPLY 1

I am having a difficult time understanding your data, but I will assume the following. You have multiple nodes connected to a root node, which you are identifying as a node with label INTID and name property equal to "575". You want to form a parent/child relationship among the nodes connected to the root node, where the parent is the node with the highest overall property value, and the remaining nodes are children connected to the parent with an outgoing MASTER_OUT relationship to the parent. If this is correct, I believe the following query will form the parent/child relationship for the given root node. If not correct, please correct my misunderstanding of your data and I will try to adjust the query.

match (n:PERSON)-->(i:INTID{name:"575"})
with n
order by n.overall desc
limit 1
with n
match (m:PERSON)-->(i:INTID{name:"575"}) where m<>n
with n, m
merge (m)-[:MASTER_OUT]->(n)
return n,m