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.

Duplicate edges

Hi,

I have two .csv files contaning information about the nodes and edges I want to import. The node .csv has columns "id, name" where is an integer and name is a string. The edge .csv has columns "A_id, B_id, type", where "A_id" is the id of the source node for the edge and B_id is the id for the connected node and "type"  for the relationship. The trouble is I only need one edge connecting two nodes where the direction doesn't matter, but each edge has a duplicate that only transposes A_id and B_id. This creates an additional relationship connecting two nodes in the opposite direction. 

A_ID, B_ID

1 , 2
1 , 3
1 , 4
2 , 1
2 , 5
4 , 1

How do I load the edges while discarding the duplicates? I'm using the following query:  

 

:auto LOAD CSV WITH HEADERS FROM "file:///edges.csv" AS row
CALL {
WITH row
MATCH (a:Company {id: toInteger(row.A_id)}), (b:Company {id: toInteger(row.B_id)})
CREATE (a)-[e:COMPETES_WITH {role: row.TYPE}]->(b)
} IN TRANSACTIONS of 2 ROWS
 
 
 

 

2 REPLIES 2

If it is ensured that each pair of id's is presented in the csv with both permutations and the direction does not matter, you can choose to create the relationship for just one ordering and ignore the other ordering. The following query created the relationship only when A_ID < B_ID. 

auto LOAD CSV WITH HEADERS FROM "file:///edges.csv" AS row
CALL {
WITH row
WITH row, toInteger(row.A_id) as a_id, toInteger(row.B_id) as b_id
WHERE a_id < b_id
MATCH (a:Company {id: a_id}), (b:Company {id: b_id})
CREATE (a)-[e:COMPETES_WITH {role: row.TYPE}]->(b)
} IN TRANSACTIONS of 2 ROWS

I have an alternative solution if the assumption is not correct, just let me know.  

Sorry...it looks like I did not copy the colon when copy/pasting the code from my Neo4j Browser. The edit feature is again acting up, so I can correct it.