apoc.neighbors.tohop

Procedure APOC Core

apoc.neighbors.tohop(node, rel-direction-pattern, distance) - returns distinct nodes of the given relationships in the pattern up to a certain distance, can use '>' or '<' for all outgoing or incoming relationships

Signature

apoc.neighbors.tohop(node :: NODE?, types =  :: STRING?, distance = 1 :: INTEGER?) :: (node :: NODE?)

Input parameters

Name Type Default

node

NODE?

null

types

STRING?

distance

INTEGER?

1

Output parameters

Name Type

node

NODE?

Usage Examples

The examples in this section are based on the following sample graph:

MERGE (mark:Person {name: "Mark"})
MERGE (praveena:Person {name: "Praveena"})
MERGE (joe:Person {name: "Joe"})
MERGE (lju:Person {name: "Lju"})
MERGE (michael:Person {name: "Michael"})
MERGE (emil:Person {name: "Emil"})
MERGE (ryan:Person {name: "Ryan"})

MERGE (ryan)-[:FOLLOWS]->(joe)
MERGE (joe)-[:FOLLOWS]->(mark)
MERGE (mark)-[:FOLLOWS]->(emil)
MERGE (michael)-[:KNOWS]-(emil)
MERGE (michael)-[:KNOWS]-(lju)
MERGE (michael)-[:KNOWS]-(praveena)
MERGE (emil)-[:FOLLOWS]->(joe)
MERGE (praveena)-[:FOLLOWS]->(joe)

The apoc.neighbors.tohop procedure compute a node’s neighborhood up to a specified hop count.

The following returns the people that Praveena FOLLOWS up to 1 hop:

MATCH (p:Person {name: "Praveena"})
CALL apoc.neighbors.tohop(p, "FOLLOWS>", 1)
YIELD node
RETURN node
Table 1. Results
nodes

(:Person {name: "Joe"})

The only person that Praveena follows is Joe, so that’s the only node returned. What about if we include people at up to 2 hops?

The following returns the people that Praveena FOLLOWS up to 2 hops:

MATCH (p:Person {name: "Praveena"})
CALL apoc.neighbors.tohop(p, "FOLLOWS>", 2)
YIELD node
RETURN node
Table 2. Results
nodes

(:Person {name: "Mark"})

(:Person {name: "Joe"})

Now Mark is returned as well. The following graph patterns describe how Emil knows the different people:

  • (praveena)-[:FOLLOWS]-(joe)

  • (praveena)-[:FOLLOWS]-(joe)-[:FOLLOWS]→(mark)

And if we just want a count of the number of people, we can use the count variant.

The following returns the number of people that Praveena FOLLOWS up to 2 hops:

MATCH (p:Person {name: "Praveena"})
CALL apoc.neighbors.tohop.count(p, "FOLLOWS>", 2)
YIELD value
RETURN value
Table 3. Results
value

2

If we aren’t interested in knowing which nodes are in our neighborhood, but just want a count of the number, we can do that as well. See apoc.neighbors.tohop.count.