apoc.neighbors.athop

Procedure APOC Core

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

Signature

apoc.neighbors.athop(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)

This procedure computes a node’s neighborhood at a specific hop count.

The following returns the people that Emil KNOWS at 1 hop:

MATCH (p:Person {name: "Emil"})
CALL apoc.neighbors.athop(p, "KNOWS", 1)
YIELD node
RETURN node
Table 1. Results
node

(:Person {name: "Michael"})

Emil only has a direct KNOWS relationship to Michael, so Michael is the only node returned by this query.

The following returns the people that Emil KNOWS at 2 hops:

MATCH (p:Person {name: "Emil"})
CALL apoc.neighbors.athop(p, "KNOWS", 2)
YIELD node
RETURN node
Table 2. Results
node

(:Person {name: "Praveena"})

(:Person {name: "Lju"})

Michael also KNOWS Praveena and Lju, and since Emil doesn’t KNOW either of those directly, he only KNOWS them at a hop distance of 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.athop.count.