3
votes

I can load CSV into Neo4j for a specific label (say PERSON) and the nodes are created under the label PERSON.

I also have another CSV to illustrate the relationships between the person and it looks like:

name1, relation, name2
a, LOVE, b
a, HATE, c

I want to create a relationship between these pairs and the relationship thus created should be "LOVE", "HATE", etc, instead of a rigid RELATION as done by the below script:

load csv with headers from "file:///d:/Resources/Neo4j/person-rel.csv" as p
match (a:PERSON) where a.name=p.name1
match (b:PERSON) where b.name=p.name2
merge (a)-[r:REL {relation: p.REL}]->(b)

By doing this, I have a bunch of REL-type relations but not LOVE- and HATE-relations.

In another word, I want the REL in the last line of the script to be dynamically assigned. And then I can query out all the relationship types using Neo4j API.

Is this possible?

1

1 Answers

6
votes

You can install the APOC library and then use apoc.merge.relationship

apoc.merge.relationship(startNode, relType, {key:value, ...}, {key:value, ...}, endNode) - merge relationship with dynamic type

load csv with headers from "file:///d:/Resources/Neo4j/person-rel.csv" as p
match (a:PERSON) where a.name=p.name1
match (b:PERSON) where b.name=p.name2
call apoc.merge.relationship(a,p.REL,{},{},b) yield rel
return count(*);