Bharat Ram Ammu
Bharat Ram Ammu

Reputation: 184

Adding relationship to existing nodes with Cypher doesn't work

I am working on Panama dataset using Neo4J graph database 1.1.5 web version. I identified Ion Sturza, former Prime Minister of Moldova on the database and want to make a map of his related network. I used following code to query using Cypher (creating a variable 'IonSturza'):

MATCH (IonSturza {name: "Ion Sturza"}) RETURN IonSturza

I identified that the entity 'CONSTANTIN LUTSENKO' linked differently to entities like 'Quade..' and 'Kinbo...' with a name in small letters as in this picture. I hence want to map a relationship 'SAME_COMPANY_AS' between the capslock and the uncapped version. I tried the following code based on this answer by @StefanArmbruster:

MATCH (a:Officer {name :"Constantin Lutsenko"}),(b:Officer{name : 
"CONSTANTIN LUTSENKO"})
where (a:Officer{name :"Constantin Lutsenko"})-[:SHAREHOLDER_OF]-> 
(b:Entity{id:'284429'})
CREATE (a)-[:SAME_COMPANY_AS]->(b)

Instead of indexing, I used the 'where' statement to specify the uncapped version which is linked only to the entity bearing id '284429'. My code however shows the cartesian product error message:

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))<<

Also when I execute, there are no changes, no rows!! What am I missing here? Can someone please help me with inserting this relationship between the nodes. Thanks in advance!

Upvotes: 1

Views: 1253

Answers (2)

Bharat Ram Ammu
Bharat Ram Ammu

Reputation: 184

I tried the answer by @InverseFalcon and thanks to it, by modifying the property identifier from 'id' to 'name' and using the property for both 'a' and 'b', 4 relationships were created by the following code:

MATCH (a:Officer {name :"Constantin Lutsenko"})-[:SHAREHOLDER_OF]-> 
(:Entity{name:'KINBOROUGH PORTFOLIO LTD.'}),(b:Officer{name : "CONSTANTIN 
LUTSENKO"})-[:SHAREHOLDER_OF]->(:Entity{name:'Chandler Group Holdings Ltd'})
CREATE (a)-[:SAME_NAME_AS]->(b)

Thank you so much @InverseFalcon!

Upvotes: 0

InverseFalcon
InverseFalcon

Reputation: 30397

The cartesian product warning will appear whenever you're matching on two or more disconnected patterns. In this case, however, it's fine, because you're looking up both of them by what is likely a unique name, s your result should be one node each.

If each separate part of that pattern returned multiple nodes, then you would have (rows of a) x (rows of b), a cartesian product between the two result sets.

So in this particular case, don't mind the warning.

As for why you're not seeing changes, note that you're reusing variables for different parts of the graph: you're using variable b for both the uppercase version of the officer, and for the :Entity in your WHERE. There is no node that matches to both.

Instead, use different variables for each, and include the :Entity in your match. Also, once you match to nodes and bind them to variables, you can reuse the variable names later in your query without having to repeat its labels or properties.

Try this:

MATCH (a:Officer {name :"Constantin Lutsenko"})-[:SHAREHOLDER_OF]-> 
(:Entity{id:'284429'}),(b:Officer{name : "CONSTANTIN LUTSENKO"})
CREATE (a)-[:SAME_COMPANY_AS]->(b)

Though I'm not quite sure of what you're trying to do...is an :Officer a company? That relationship type doesn't quite seem right.

Upvotes: 3

Related Questions