Good day,
I trust you are well.
I am new to Neo4j and struggling to create relationships between nodes. I was able to do so a few years ago with my cuurent cypher code, but it seams my code is not working.
I created the following nodes:
create (n1:Female {id:1, name:'Giovanna Bell', age:57})
create (n101:Service_ID {Id:101, Name:'MED'})
Now i wanted to created a relationship:
MATCH (a:Female {name:'Giovanna Bell'}),(b:Service_ID {name:'MED'}) MERGE (a)-[p:SERVICE]->(b)
But then I get the 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))
Could you kidly assist?
Thank you and kindest regards,
Emil
@emilvermaas
What version of Neo4j is in play here?
How are you submitting the cypher? via Neo4j Browser? cypher-shell ? or ??
create (n1:Female {id:1, name:'Giovanna Bell', age:57})
create (n101:Service_ID {Id:101, Name:'MED'})
MATCH (a:Female {name:'Giovanna Bell'}),(b:Service_ID {name:'MED'}) MERGE (a)-[p:SERVICE]->(b)
will create a cartesian join and the message you describe is generally a warning and not an error. After running such what is the outcome/result of
match (a:Female)-[:SERVICE]->(b) return a,b;
To supplement Dana's reply, when your goal is to lookup two nodes (that do not have a relationship in your MATCH pattern between them) with the intent to create a relationship between the nodes, this approach (cartesian product between two disconnected patterns) is the correct (and only) option.
If we assume the properties you're using for lookup will find only one node each, then it will be a cartesian product of 1 x 1 = 1 , which is just fine (though you should absolutely have indexes in place to speed up your lookups).
If you had not used properties to narrow the results of your lookup, or the nodes resulting in the lookups were too broad (many nodes instead of 1 or a few) then there could be a major impact (such as every :Female node x every :Service_ID node), which is more like the cases where the warning would apply.
@emilvermaas also your MATCH (a:Female {name:'Giovanna Bell'}),(b:Service_ID {name:'MED'}) MERGE (a)-[p:SERVICE]->(b)
is incorrect as it needs to be
MATCH (a:Female {name:'Giovanna Bell'}),(b:Service_ID {Name:'MED'}) MERGE (a)-[p:SERVICE]->(b)
and the key difference is the property is 'Name' and not 'name' as from the :Service_ID labelled node
and in short and with Neo4j 5.6.0
./cypher-shell
Connected to Neo4j using Bolt protocol version 5.0 at neo4j://localhost:7687.
Type :help for a list of available commands or :exit to exit the shell.
Note that Cypher queries must end with a semicolon.
@neo4j> create (n1:Female {id:1, name:'Giovanna Bell', age:57});
0 rows
ready to start consuming query after 234 ms, results consumed after another 0 ms
Added 1 nodes, Set 3 properties, Added 1 labels
@neo4j> create (n101:Service_ID {Id:101, Name:'MED'});
0 rows
ready to start consuming query after 32 ms, results consumed after another 0 ms
Added 1 nodes, Set 2 properties, Added 1 labels
MATCH (a:Female {name:'Giovanna Bell'}),(b:Service_ID {Name:'MED'}) MERGE (a)-[p:SERVICE]->(b);
0 rows
ready to start consuming query after 109 ms, results consumed after another 0 ms
Created 1 relationships
@neo4j> match (a:Female)-[:SERVICE]->(b) return a,b;
+------------------------------------------------------------------------------------------+
| a | b |
+------------------------------------------------------------------------------------------+
| (:Female {name: "Giovanna Bell", id: 1, age: 57}) | (:Service_ID {Id: 101, Name: "MED"}) |
+------------------------------------------------------------------------------------------+
1 row
ready to start consuming query after 67 ms, results consumed after another 5 ms
@neo4j>