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.

Creating 2 unique nodes per row from a CSV

I have some data in csv format. Each row describes a relationship between two nodes. Nodes can have multiple relationships with other nodes, although there is only one relationship per row.

For example this is a relationship derived from one row in the csv:

(Entity1:Name {Type,Details})-[Relationship Type]->(Entity2:Name {Type,Details})

Entity1 could, on a different row be in the place of Entity2 and vice versa. Entity1 could also have relationships with Entity3 and Entity4 and so on although its type will never change.

Using Py2Neo I can import and create the relationships, but end up with loads of duplicate nodes for the entities. I want each Entity node to be unique. Any suggestions?

1 REPLY 1

In Cypher, the pattern for this is usually

MERGE <first node>
MERGE <second node>
MERGE <relationship between them>

If the type and detail properties uniquely identify a node, then subsequent MERGE operations from other rows with those same properties will reuse the previously-merged node instead of creating a duplicate. Only after the start and end nodes from the first two merges are done can you MERGE the relationship between them.

The thing you do NOT want to do is do a single MERGE for the entire pattern, as that creates duplicates (MERGE is like doing a MATCH on the entire pattern, and if doesn't exist it will CREATE the entire pattern, excepting elements from already-bound variables).