Reputation: 844
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
Upvotes: 0
Views: 191
Reputation: 9284
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
Upvotes: 1