0
votes

I am trying to write a query to return all related nodes related by "IMMEDIATE_FAMILY_MEMBER"

Graph

This is my query so far

MATCH (f:NaturalPerson)-[r:IMMEDIATE_FAMILY_MEMBER*1..6]-(t)
WHERE f.Name="Jacob"
RETURN f AS fromNode, t AS toNode, r AS Metadata

Initially i thought it worked quite well, but as soon as i added a the child Thuthukile (Parents Jacob and Nkosazana) i get "duplicate" results.

At the moment the query will return a pair of related nodes and all the relationships that were traversed to link them together (ie the metadata).

How do i change this query so i return a distinct pair of nodes with the shortest path (all the relationships) between them.

Also as an extra question, is it possible to specify an or for the label of the relation itself. Ie, the same query but also include the :KNOWS relationship

Edit:

cybersams answer was correct, i made one small change to get the result i wanted.

My final query was this

MATCH (f:NaturalPerson)-[r:IMMEDIATE_FAMILY_MEMBER*..6]-(t)
WHERE f.Name="Jacob" AND t.Name<>"Jacob"
WITH f, t, r
ORDER BY SIZE(r)
RETURN f AS fromNode, t AS toNode, COLLECT(r)[0] AS Metadata

I needed to exclude the "from person" as a destination as i was not interested in the shortest path back to the parent

1
You are getting 'duplicate' results, because your query is effectively asking for all the different paths up to 6 hops from Jacob. For the example of Thuthukile, there are two different paths from Jacob, Jacob<--Thuthkile, and Jacob<--Nkosazana-->Thuthkile.Lju
Thanks @Lju, i intuitively understood that part i just did not understand how to use collect to resolve the issueDarryn Hosking

1 Answers

1
votes

Aside: why is it NaturalPerson? Are there "unnatural" people in the DB as well?

This should work:

MATCH (f:NaturalPerson)-[r:IMMEDIATE_FAMILY_MEMBER*..6]-(t)
WHERE f.Name="Jacob"
WITH f, t, r
ORDER BY SIZE(r)
RETURN f AS fromNode, t AS toNode, COLLECT(r)[0] AS Metadata

The query sorts by the length of the paths found, uses the aggregating function COLLECT to get a list of all the r paths for a given pair of f and t values, and uses the first (i.e., the shortest) path in each list.