Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
05-25-2019 02:44 AM
Hello, can I get the answer or explanations of this..?
Why the following cypher return different values..? Thanks.
Cypher 1: MATCH p = (n)-[r]->() RETURN COUNT(r)
it returns 94.
Cypher 2: MATCH (n)-[r]-() RETURN COUNT(r)
it returns 188.
Cypher 3: MATCH p = (n)-[r:property*]->() RETURN COUNT(r)
it return 113288.
My graph shown like this:
Thanks.
Im Sorry, i new in neo4j.
Solved! Go to Solution.
05-25-2019 11:00 AM
Sure, this should clear things up:
MATCH p = (n)-[r]->() RETURN COUNT(r)
94
This is equivalent to the count of all distinct relationships in your graph (assuming this is the entire query, nothing before this).
MATCH (n)-[r]-() RETURN COUNT(r)
188
All relationships in Neo4j are directed. When you leave off the direction, then a path can be matched from either direction. This means the n
here can either be the starting node of the relationship (where it comes from) or the end node of the relationship (where it points to), so each relationship will therefore be counted twice (94 * 2 = 188). If you wanted to get the number of distinct relationships instead, still leaving out the direction, you could do RETURN count(DISTINCT r)
instead. Another thing to remember is that paths in Neo4j are ordered. Even if the two nodes and single relationship are the same, the difference in the ordering of the nodes makes them distinct paths.
MATCH p = (n)-[r:property*]->() RETURN COUNT(r)
113288
This is a variable-length pattern, meaning it will match to paths of any and all lengths (minimum of 1) provided all relationships are outgoing :property relationships. In this case r
isn't referring to a single relationship, but rather a collection of relationships per path. COUNT(r)
in this case is not counting relationships, but counting the collection of relationships (not the size of the collection) per path, and since there is only one collection of relationships per path, this is the same as COUNT(n)
or COUNT(p)
or COUNT(*)
, and in all these cases will be returning you the number of paths found that match the pattern.
05-25-2019 11:00 AM
Sure, this should clear things up:
MATCH p = (n)-[r]->() RETURN COUNT(r)
94
This is equivalent to the count of all distinct relationships in your graph (assuming this is the entire query, nothing before this).
MATCH (n)-[r]-() RETURN COUNT(r)
188
All relationships in Neo4j are directed. When you leave off the direction, then a path can be matched from either direction. This means the n
here can either be the starting node of the relationship (where it comes from) or the end node of the relationship (where it points to), so each relationship will therefore be counted twice (94 * 2 = 188). If you wanted to get the number of distinct relationships instead, still leaving out the direction, you could do RETURN count(DISTINCT r)
instead. Another thing to remember is that paths in Neo4j are ordered. Even if the two nodes and single relationship are the same, the difference in the ordering of the nodes makes them distinct paths.
MATCH p = (n)-[r:property*]->() RETURN COUNT(r)
113288
This is a variable-length pattern, meaning it will match to paths of any and all lengths (minimum of 1) provided all relationships are outgoing :property relationships. In this case r
isn't referring to a single relationship, but rather a collection of relationships per path. COUNT(r)
in this case is not counting relationships, but counting the collection of relationships (not the size of the collection) per path, and since there is only one collection of relationships per path, this is the same as COUNT(n)
or COUNT(p)
or COUNT(*)
, and in all these cases will be returning you the number of paths found that match the pattern.
05-25-2019 11:40 AM
Thanks for the amazing answer!
I'm very impressed with what Neo4j could do.
All the sessions of the conference are now available online