Counting Relationships in Neo4j

When working with different graphs in Neo4j, it is often important to count the number of relationships between different nodes. Below are examples of common relationship counting tasks. The data for these examples can be found here and the script to load this data into Neo4j can be found here.

Counting Total Relationships (Edges)

MATCH (n)-[r]->() RETURN COUNT(r)

The above Cypher query will return the count of total relationships in a Neo4j database. Make sure to specify directed relationships (using “->”). A slightly different query, MATCH (n)-[r]-() RETURN COUNT(r), indicates undirected relationships, and will cause each directed relationship to be counted twice.

Counting Outgoing Relationships

MATCH (n)-[r]->() WHERE n.username = 'rafaelnadal' RETURN COUNT(r)

Here is an example of counting the number of outgoing edges from our node that represents Rafael Nadal. In the context of the Twitter data being used, this query would represent the number of users in our dataset that Rafael Nadal follows on Twitter.

Although not necessary for this example, the name of one or more specific relationship types can be specified in the query. The example below will return the same results, but does specify the FOLLOWS relationship type to be used in the query.

MATCH (n)-[r:FOLLOWS]->() WHERE n.username = 'rafaelnadal' RETURN COUNT(r)

Counting Incoming Relationships

MATCH (n)<-[r]-() WHERE n.username = 'rafaelnadal' RETURN COUNT(r)

Here is an example of counting the number of incoming edges to our node that represents Rafael Nadal. In the context of the Twitter data being used, this query would represent the number of users in our dataset that follow Rafael Nadal on Twitter.

Although not necessary for this example, the name of one or more specific relationship types can be specified in the query. The example below will return the same results, but does specify the FOLLOWS relationship type to be used in the query.

MATCH (n)<-[r:FOLLOWS]-() WHERE n.username = 'rafaelnadal' RETURN COUNT(r)

Counting Both Incoming and Outgoing Relationships

MATCH (n)-[r]-() WHERE n.username = 'rafaelnadal' RETURN COUNT(r)

This example is very similar to the previous two. The only difference is that the relationship direction has been removed. This will cause both incoming and outgoing relationships to be counted.

Counting Nodes with No Relationships

MATCH (n) WHERE NOT (n)--() RETURN COUNT(n)

The Cypher query above will match and count nodes with no incoming or outgoing relationships. Change the WHERE clause to WHERE NOT (n)-->() to specify outgoing relationships only, or to WHERE NOT (n)<--() to specify incoming relationships only.

Another approach to finding nodes with no relationships is to use OPTIONAL MATCH. OPTIONAL MATCH will search for a particular pattern, and return NULL for nodes not matching this pattern.

MATCH (n) OPTIONAL MATCH (n)--(x) WITH n, x WHERE x IS NULL RETURN COUNT(n)

One thought on “Counting Relationships in Neo4j”

  1. Michael Hunger

    Thanks a lot for the blog post.

    It helps a lot to use labels + an index when looking up nodes by property, text or range queries.

    And for just counting relationships this is faster (it reads the transactional relationship-count)

    MATCH (n:Person) WHERE n.username = ‘rafaelnadal’ RETURN SIZE( (n)–>() ) as degree
    or
    MATCH (n:Person) WHERE n.username = ‘rafaelnadal’ RETURN SIZE( (n)-[:FOLLOWS]->() ) as follows

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">