Don Mathew
Don Mathew

Reputation: 166

How to get degree of a node while filtering using MATCH in neo4j

I have a graph with 4 levels. While filtering using MATCH, how can i get the "degree" of a node? I am always getting a "degree" of 1.

Here is my query:

MATCH (k)-[r*]->(n:ABC)
WITH k,r,n,count(k) as degree
WHERE k.Value='30 ' AND degree > 1
RETURN n,r,k,degree;

Upvotes: 7

Views: 9689

Answers (3)

danodonovan
danodonovan

Reputation: 20373

Neo4j size function is now deprecated, and apoc.node.degree is recommended instead;

MATCH (k)-[r*]->(n:ABC)
WITH k,r,n, apoc.node.degree(k) as degree
WHERE k.Value='30 ' AND degree > 1
RETURN n,r,k,degree;

NB You will need APOC to be installed on your local Neo4j instance

Upvotes: 2

Michael Hunger
Michael Hunger

Reputation: 41706

You are getting count of 1 because you aggregated over all 3, start-node, end-node, and all relationships in the path.

This is the most efficient way.

MATCH (k)
WITH k, size((k)-[:TYPE]->()) as degree
WHERE k.Value='30 ' AND degree > 1
MATCH (k)-[r:TYPE]->(n:ABC)
RETURN n,r,k,degree;

Upvotes: 12

Brian Underwood
Brian Underwood

Reputation: 10856

More information would be helpful, but in general you can get the degree of a node by doing something like:

MATCH (n)--(other)
WHERE n.id = {id}
RETURN count(other)

If you want to find degrees for many nodes you can leave out the WHERE or specify a more generic query:

MATCH (n)--(other)
WHERE n.property = {value}
RETURN n, count(other)

Upvotes: 1

Related Questions