Creating relationship (with properties) from CSV between two groups of existing nodes

Hi there

I am trying to create a relationship between two groups of nodes to indicate the association type between Clients (node type 1) and Related Parties (node type 2). I have successfully added both node types with their properties and now would like to create a relationship between the Clients and the Related Parties i.e. Related Party XYZ is a "parent company" of Client Company ABC (relationship is in inverted commas and may differ for different relationships - field RELATED PARTY_TYPE). I would like the Related Party type to be on the arrow in the graph and to add properties to the relationship so that when the relationship is clicked on, the properties appear. My relationship CSV looks like the following;

CLIENT_ID, RELATED PARTY_ID, RELATED PARTY_TYPE, PERCENT_OWNED, UBO_SHAREHOLDING_TYPE, SHAREHOLDING_LEVEL, UBO_ROLES

49453, 664, Parent Company, 24.5, Indirect, 4, Party in Ownership Chain

I have tried many Cypher variations and none have worked so far including the below but I get a Cartesian product warning " This query builds a cartesian product between disconnected patterns."

USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS FROM 'file:///Relationships.csv' AS csvLine
MATCH (c:Client {CLIENT_ID: toInteger(csvLine. TECH_CLIENT_ID)}),(r:RelatedParty {RELATEDPARTY_ID: toInteger(csvLine. RELATEDPARTY_ID)})
CREATE (c)-[:AssociatedWith {RELATEDPARTY_TYPE: csvLine.RELATEDPARTY_TYPE}]->(r)

Any help would be much appreciated.

I have tried the below but I get a Cartesian product warning " This query builds a cartesian product between disconnected patterns."

USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS FROM 'file:///Relationships.csv' AS csvLine
MATCH (c:Client {CLIENT_ID: toInteger(csvLine. TECH_CLIENT_ID)}),(r:RelatedParty {RELATEDPARTY_ID: toInteger(csvLine. RELATEDPARTY_ID)})
CREATE (c)-[:AssociatedWith {RELATEDPARTY_TYPE: csvLine.RELATEDPARTY_TYPE}]->(r)

In this case you can safely ignore the warning. Since you do match on an id both for Client and RelatedParty - which should give you 1 result each. So the cartesian product is 1 x 1 = 1.

Hi Stefan

Thanks for your response. Unfortunately the cypher query does not work as Neo4j can't execute it fully. As seen below the 'loading' icon appears and the relationships are never formed (even after left 'loading' for days). Is there another query you are able to suggest to create relatioships between two types of nodes?

Thanks and regards
Rebecca

There are some issues with your .csv file and the Cypher code.

1: The header and data lines have a space after comma.

2: Not referencing the headers correctly. RELATED_PARTY_ID is referenced as RELATEDPARTY_ID in the MATCH statement.

Here is how you can fix this:

.csv file:

TECH_CLIENT_ID, RELATED_PARTY_ID, RELATED_PARTY_TYPE, PERCENT_OWNED, UBO_SHAREHOLDING_TYPE, SHAREHOLDING_LEVEL, UBO_ROLES
49453, 664, Parent Company, 24.5, Indirect, 4, Party in Ownership Chain

-See the spaces after comma in the headers and data lines. I don't know whether the first column also has space before. But, for consistency I included the space.

Now when you refer the headers you need to use back tick before and after the column name to include the empty space.

Here is the corrected Cypher queries:

LOAD CSV WITH HEADERS FROM 'file:/rels.csv' AS csvLine
MERGE (c:Client {CLIENT_ID: toInteger(csvLine. TECH_CLIENT_ID)})
MERGE (r:RelatedParty {RELATEDPARTY_ID: toInteger(trim(csvLine. RELATED_PARTY_ID))})

return c, r

Result:
05%20PM

Create relationship:

LOAD CSV WITH HEADERS FROM 'file:/rels.csv' AS csvLine
MATCH (c:Client {CLIENT_ID: toInteger(csvLine. TECH_CLIENT_ID)})
MATCH (r:RelatedParty {RELATEDPARTY_ID: toInteger(trim(csvLine. RELATED_PARTY_ID))})
CREATE (c)-[:AssociatedWith {RELATEDPARTY_TYPE: csvLine. RELATEDPARTY_TYPE}]->(r)
return c, r

result:
17%20PM

Hi ameyasoft

Thanks for your message - but those issues are only within my post due to rushed typing and not my cypher query. The query is being executed, the relationship query is not giving any errors but as seen in the image above but is taking hours/days to load.

Regards
Rebecca