Search code examples
cypher

How do I do a WHERE clause on a COUNT in Cypher?


This is what I'm trying:

match 
(e:Person) - [r] - (f:Person) 
where (count(r) > 5 AND count (r) <10) 
return id(e), e.name; 

I get

QueryExecutionKernelException: Invalid use of aggregating function count(...) in this context

Basically I'm wanting to find a Person who is related to between 5 and 10 other Persons.


Solution

  • to find out people who are connected with each other by more than relationship ex:

    • a likes b
    • a knows b
    • a lives_with b

    use

    match (a:Person)-[r]-(b:person)
    with a,b,count(r) as cnt
    where cnt > 5 and cnt < 10
    return *
    

    if however you want to find people who are connected as a chain ( friends of friends )

    • a knows b
    • b knows c
    • c likes d
    • d knows e

    and you want to find a to d then you can use something like

    MATCH (n:Person)-[r*1..3]->(m:Person)
    RETURN *
    

    relevant tutorial here