0
votes

Using Cypher, how can I find nodes that have x or more relationships of distinct types, and are connected to y or more different nodes?

For instance, a:Person can be connected to b:Person, via relationships of type 'family','friend','coworker'.

How do we find a, such that:

  • a has 2 or more distinct relationship types,
  • a is connected to at least 10 other people
1

1 Answers

1
votes
  1. a has 2 or more distinct relationship types
  2. a is connected to at least 10 other people

use this query with intermediate aggregation:

MATCH (p:Person)-[r:FAMILY|:FRIEND|:COWORKER]->(other:Person)
WITH p, count(distinct type(r)) as c, count(distinct other) as people
WHERE c > 2 and people >= 10
RETURN p

(you can also leave off the provided rel-types in the query)

Only for the sizes you can use a path expression, which is faster.

MATCH (p:Person)
WHERE SIZE((p)-[:FAMILY|:FRIEND|:COWORKER]->()) >= 10
RETURN p