简体   繁体   中英

How to use cypher to create multiple edges for a graph in neo4j?

在此处输入图像描述

I want to create a data graph in neo4j with cypher like the one in this figure.

create (v1:D)-[]->(v2:C)
create (v1:D)-[]->(v3:A)
create (v1:D)-[]->(v4:B)
create (v2:C)-[]->(v1:D)
create (v2:C)-[]->(v3:A)
create (v3:A)-[]->(v1:D)
create (v3:A)-[]->(v2:C)
create (v4:B)-[]->(v1:D)

Is it possible to create such a graph in cypher rather than using csv import from neo4j? One thing we need is the identifiers for the created nodes.

update: the neo4j version is 5.3.0

You are using CREATE command, that's why multiple nodes are getting created. Try using MERGE :

MERGE (a:A{id: randomUUID()})
MERGE (b:B{id: randomUUID()})
MERGE (c:C{id: randomUUID()})
MERGE (d:D{id: randomUUID()})
MERGE (a)-[r1:R{id: randomUUID()}]->(b)
MERGE (b)-[r2:R{id: randomUUID()}]->(c)
MERGE (a)-[r3:R{id: randomUUID()}]->(c)
MERGE (c)-[r4:R{id: randomUUID()}]->(d)
RETURN a,b,c,d, r1,r2,r3,r4

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM