inersha
inersha

Reputation: 462

Neo4j: Is there a fast way to get the distance between two nodes?

I have nodes in a chain, like this:

(a)<-[:rel]-(b)<-[:rel]-(c) ... (x)

Is there a fast way to count the number of nodes (or relationships) between (a) and (x), even if there are thousands of nodes between them? So far p=(a)<-[:rel*]-(x) has been "slow".

Note: I do not need to know anything about the nodes in between, I just want to find the distance.

Upvotes: 2

Views: 5604

Answers (1)

J Fabian Meier
J Fabian Meier

Reputation: 35825

Use the shortestPath method:

MATCH (martin:Person { name:"Martin Sheen" }),(oliver:Person { name:"Oliver Stone" }), 
p = shortestPath((martin)-[*..15]-(oliver))
RETURN length(p)

https://neo4j.com/docs/developer-manual/current/cypher/#query-shortest-path

Upvotes: 5

Related Questions