Creating relationships between nodes of the same kind (LOAD CSV)

Hi there

I am trying to associate nodes of the same kind/label but struggling with the correct cypher. I have a list of companies and I am trying to associate them based on an association type i.e. Company ABC is a shareholder of Company XYZ etc.

My Node CSV looks like the following;

LegalEntityID,LegalEntityName,LegalEntitySubType
225176,Company ABC,Financial Institution
52489,Company XYZ,Commercial Corporate

And the cypher query used to create the nodes is;
LOAD CSV WITH HEADERS FROM "file:///Nodes.csv" AS csvLine
CREATE (e:Entity {Id: toInteger(csvLine.LegalEntityID), LegalEntityName: csvLine.LegalEntityName, LegalEntitySubType: csvLine.LegalEntitySubType})

My Relationship CSV looks like the following;

RelationshipID,LegalEntityID1,LegalEntityID2,AssociationType,HierarchyLevel
285378,52489,225176,Shareholder,1

The issue is that I do not know how to create a relationship between two nodes that are linked on the same ID (LegalEntityID) thus I know it is incorrect to have LegalEntityID1 and LegalEntityID2 in the relationship file but I am not sure how to associate the entities otherwise.

I tried the following cypher query however the response is "(no changes, no records)";
LOAD CSV WITH HEADERS FROM 'file:///Relationships.csv' AS csvLine
MATCH (e1:Entity {Id:csvLine.LegalEntityID1}), (e2:Entity {Id:csvLine.LegalEntityID2})
MERGE (e2)-[:Associated_to]->(e1)

Due to the large number of relationships, this needs to be one by loading a CSV.

Please assist if you are able to, any help would be much apprciated.

Thanks and regards
Rebecca

Your approach looks correct, though you will want to make sure you have an index present on :Entity(Id) for fast lookup of entities by their id, and you'll want to use periodic commits in your LOAD CSV.

Hi Andrew

Thanks for yor response. I have done both of those and unfortunately get the same result of '(no changes, no records)'.

Regards
Rebecca

In that case there's either something wrong with the properties you're expecting in your CSV (do a LOAD CSV with RETURN csvLine LIMIT 1 to confirm the properties present, look out for trailing whitespace and case), or you aren't doing necessary conversions (all properties from a csv are Strings by default...if the Id property of an :Entity is supposed to be numeric, you need to use toInteger() around the csv property), or there's something wrong with your :Entity nodes in your graph (make sure the property names have the right case and don't contain extra whitespace).

Thanks Andrew. Looking at another discussion that you have commented on (Creating relationships conditionally among nodes of same label), should I follow the same logic whereby I create two different node groups and then merge them before creating the relationship?

You already have separate CSV files for nodes vs relationships, so you should be creating your nodes first, then doing the query for doing the MERGE of relationships between already created nodes.

I have created my nodes successfully and am now trying to create the relationships using;

LOAD CSV WITH HEADERS FROM 'file:///Relationships.csv' AS csvLine
MATCH (e1:Entity {Id:csvLine.LegalEntityID1}), (e2:Entity {Id:csvLine.LegalEntityID2})
MERGE (e2)-[:Associated_to]->(e1)

I have checked all of the following from your previous response and all is in order - " In that case there's either something wrong with the properties you're expecting in your CSV (do a LOAD CSV with RETURN csvLine LIMIT 1 to confirm the properties present, look out for trailing whitespace and case), or you aren't doing necessary conversions (all properties from a csv are Strings by default...if the Id property of an :Entity is supposed to be numeric, you need to use toInteger() around the csv property), or there's something wrong with your :Entity nodes in your graph (make sure the property names have the right case and don't contain extra whitespace)."

Still getting the same (no changes, no records). If anyone can shed some more light I would be grateful.

Regards
Rebecca

There's something going wrong there, the only place where this could fail is in the MATCHes.

At this point it's on you to sanity check this. Look only at the first couple of rows in the CSV. Look at the CSV itself. Look for whitespace. Do individual matches of the nodes you're attempting to look up, by the id that you expect (rather than using the csvLine). Look at the property keys. Make sure your indexes are up and online on :Entity(Id), and work otherwise for matches when using literals for the property values (rather that from csvLine).

Hi Andrew

As you suggested, I have simplified everything completely, loading only two nodes 'CompanyABC' and 'CompanyXYZ' with their associated unique IDs. After creating an index on LegalEntityID I successfully loaded the below in a csv;
LegalEntityID,LegalEntityName
225176,CompanyABC
12470,CompanyXYZ
(see attached screenshot - there are no whitespaces at all)
using the following:
LOAD CSV WITH HEADERS FROM "file:///test.csv" AS csvLine
CREATE (e:Entity {Id: toInteger(csvLine.LegalEntityID), LegalEntityName: csvLine.LegalEntityName})

I then tried to associate these two nodes using the below;
Match (a:LegalEntityID),(b:LegalEntityID) where a.LegalEntityID = '225176' and b.LegalEntityID = '12470' create (b)-[r:HAS_ASSOCIATION]->(a)

and I still got the reponse (no changes, no records) - see attached screenshot. I have followed everything you have said but unfortunately it still isn't working. Please let me know if there is anything else I can try.

Thanks and regards
Rebecca

csv