0
votes

I am building a graph database for a given family and I want to get the common family member(node) between two family members(two nodes).

I should say that the two nodes are not directly connected to the common requested node.

MATCH (b:Person)-[:RELATES*]->(a:Person)<-[:RELATES*]-(c:Person)
where ID(b) = 7 
and ID(c) = 50
RETURN a

a -> b -> c

d -> c

What I'm expecting when I run the query with 'a' and 'd' is to get 'c'

1

1 Answers

0
votes

This is one way.

Another way is to get all ancestor nodes from each, and then intersect the two lists:

MATCH (b:Person), (c:Person)
where ID(b) = 7 
and ID(c) = 50
WITH [(b)-[:RELATES*]->(ancestor) | ancestor] as bAncestors, [(c)-[:RELATES*]->(ancestor) | ancestor] as cAncestors
RETURN apoc.coll.intersection(bAncestors, cAncestors) as commonAncestors

This uses APOC Procedures for the intersection() function, but you can use this pure Cypher workaround instead if you don't have APOC:

...
RETURN [ancestor in bAncestors WHERE ancestor in cAncestors] as commonAncestors