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.

How to count number of nodes for which a path exists?

Rogie
Node Link

I would like to count how many (User) nodes exists for a which there is a direct relationship with a (Keyword) node.

If I do
Match (u:User)-[]->(k:Keyword) Return Count(u)
then the same user may be counted multiple times since it can have connections to different keywords. How do I account for this?

4 REPLIES 4

ameyasoft
Graph Maven
Assuming the Keyword node has a property 'name' with values: name: 'cat', name: 'dog'

Match (u:User)-[]->(k:Keyword) 
Return  distinct k.name,  Count(u)

Result: 
cat, 10
dog, 15

I'm not asking for a count over each keyword. I'm asking for the number of nodes that have a keyword relationship, regardless of name.

Try count(distinct u)

Another possibility:

MATCH (u:User)
WHERE (u)-->(:Keyword)
RETURN count(u)

Though if you know the relationship type (if only a single type connects from a :User to a :Keyword node), then you can use that in your WHERE pattern instead of the :Keyword label, and it will turn to a more efficient degree check, since a node knows what relationship types are connected to it, so you won't have to pay the cost of expanding and filtering on the label of the node on the other side.