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

1 Like

Hi,Rebecca.
I encounter the same problem now.
Did you find the solution?

Check the node labels and property names. The correct query:

Match (a:Entity)
match (b:Entity) 
where a.Id = 225176 and b.Id = 12470 
merge (b)-[:HAS_ASSOCIATION]->(a)
return a, b
 This creates the expected relation between the nodes.

Hi,
I am building a graph to show the dependency relationships between different artifacts in maven central repository. And the neo4j version is 3.5.14.
Firstly, I loaded a CSV like below and named the label as Artifact :

first cypher is:

load csv with headers from 'file:///test_header.csv' as art
create (:Artifact {gav: art.gav, groupId: art.groupId, artifactId: art.artifactId,
                   version: substring(art.version,2), packaging: art.packaging})

Then,I want to load a CSV to create relationships between nodes with same labels.
depend_test

the cypher is like this:

load csv with headers from 'file:///test_depend.csv' as art
MATCH (a:Artifact {gav: art.from}) , (b:Artifact {gav: art.to}) create (a)-[r:DEPEND_ON]->(b)

But, it returned a warning, and no relationship built.

This query builds a cartesian product between disconnected patterns.
If a part of a query contains multiple disconnected patterns, this will build a cartesian product between all those parts. This may produce a large amount of data and slow down query processing. While occasionally intended, it may often be possible to reformulate the query that avoids the use of this cross product, perhaps by adding a relationship between the different parts or by using OPTIONAL MATCH (identifier is: (b))

I also tried this one, no result too.

LOAD CSV WITH HEADERS FROM "file:///test_depend.csv" AS row
MATCH (f:Artifact {gav: row.from})
MATCH (t:Artifact {gav: row.to})
MERGE (f)-[:DEPEND_ON]->(t);

So, my question is How can I load the CSV correctly and build the relationship between these nodes with same labels?

The values posted in the first table are not visible completely. If you can share the complete values for gav and grouped may be I can help you. Looks like the from and to values are concatenated values.

sorry for that, the correct value is shown below for the first picture,

the neo4j version is 3.5.14

Thanks for the data. I recreated in my system with Neo4j 3.5.15 and it worked. Even with the warning it should execute. Looking at the data you may be having white space issue at the end of each column value. Also, I notice there is more space in the 'to' column in the second file compared to the one in the first file. Try using trim(gav) or rTrim(gav) while creating the nodes and relationships.

Yes, the problem is the white space. I already solved it. Thank you.