Reputation: 586
I have two types of nodes stored in Neo4j db: Person and Group. I defined a relationship "IN" between Person and Group: (p:Person)-[:IN]->(g:Group)
.
The question is given 2 Person nodes, how can I find the relationships between them?
I know I can use Cypher queries such as
MATCH (p1:Person{pid: '11231'})-[:IN]->(g:Group)<-[:IN]-(p2:Person{pid: '1231231'})
RETURN p1,p2,g;
But how can I describe a multi-hop relationship so that Neo4j can find links between two not directly linked Person nodes? I don't know how many hops are required to link these two Person nodes.
Upvotes: 0
Views: 1390
Reputation: 2513
You can use below Cypher query:
MATCH (p1:Person{pid: '11231'})-[:IN*]-(p2:Person{pid: '1231231'}) return p1,p2;
Note: the * in the relationship IN does the trick.
However, this is inefficient approach and you should limit the number of hops like this:
MATCH (p1:Person{pid: '11231'})-[:IN*1..5]-(p2:Person{pid: '1231231'}) return p1,p2;
More relationship match pattern can be found in the cypher refcard patterns section
https://neo4j.com/docs/cypher-refcard/current/
Upvotes: 1