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.

Import with Dynamic Relationship and Grouping

Hi all,

I have a dataset like this below -

id, seq,quotes
q1,1,a1
q1,2,a2
q1,3,a4
q1,4,b5
q2,1,a1
q2,2,a2
q2,3,a4
q2,4,b5

I want to create a graph with grouped relationship with nodes. like

(q1)-[:FIRST]->(seq:1)-[:quotes]->(a1)
(seq:1)-[:NEXT]->(seq:2)-[:quotes]->(a2)
(seq:2)-[:NEXT]->(seq:3)-[:quotes]->(a4)
(seq:3)-[:NEXT]->(seq:4)-[:quotes]->(b5)

basically where ever the seq = 1 in the csv, the relationships should have [:FIRST], and the sequence numbers, 2,3,4 must be connected via [:NEXT], to their previous seq numbers.

Appreciate help.

Thanks,

1 REPLY 1

ameyasoft
Graph Maven
I created a .csv file with the first four values. Try this:

LOAD CSV WITH HEADERS FROM "file:/amelia.csv" AS row
with row

merge (a:Qid {name: row.id})
merge (s:Seq {seq: toInteger(row.seq)})
merge (q:Quotes {quote: row.quotes})

with a, s, q, row

FOREACH(ignoreMe IN CASE WHEN toInteger(row.seq) = 1 THEN [1] ELSE [] END|
	
	merge (a)-[:FIRST]->(s)-[:QUOTES]->(q)

)

FOREACH(ignoreMe IN CASE WHEN toInteger(row.seq) > 1 THEN [1] ELSE [] END|
	
	merge (sq:Seq {seq: toInteger(row.seq) - 1})
	merge (sq)-[:NEXT]->(s)-[:QUOTES]->(q)
	

)

Result:
2X_4_4c75bad182734a0416c43b51e1f3414c2561523c.png