cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Different About Relations Count() in Cypher

ardan7779
Node Clone

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:


Where all the relations have same label "property".

Thanks.
Im Sorry, i new in neo4j.

1 ACCEPTED SOLUTION

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.

View solution in original post

2 REPLIES 2

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.

Thanks for the amazing answer!
I'm very impressed with what Neo4j could do.