1
votes

I'm new to neo4j. So I was making my family tree on neo4j and have a 'Person' node which has properties related to a person such as a name, date of birth, place of birth, etc. It also has an array property called medical history which will have an array of diseases.

Now I want to check if any disease has been passed on to a person from his family. So I have a query,

MATCH p=(k:Person{name :"kristy frank"})-[r:FATHER_OF | MOTHER_OF *1..7 ]-(l:Person)
where SINGLE(x IN l.diseases WHERE x = "diabetes")  
RETURN k,r,l

This returns all the nodes in my family that have diabetes. But there's a probability that one or two nodes in between may not have diabetes. So I want the query to be generic to search along the family tree and find the nodes that could have diabetes as a property even if 2-3 nodes in between are skipped.

1

1 Answers

0
votes

This query will return all paths of up to length 7 in the tree rooted at "kristy frank", where any person in the path has diabetes:

MATCH p=(:Person{name :"kristy frank"})-[:FATHER_OF|MOTHER_OF*..7]->(:Person)
WHERE ANY(p IN NODES(p) WHERE "diabetes" IN p.diseases)
RETURN p;

Note, however, that the above query's resulting paths can include paths that are completely overlapped by longer paths.

To avoid that, you can instead return all tree paths that either end at a leaf node or are of length 7:

MATCH p=(:Person{name :"kristy frank"})-[:FATHER_OF|MOTHER_OF*..7]->(l:Person)
WHERE
  (LENGTH(p) = 7 OR NOT (l)-[:FATHER_OF|MOTHER_OF]->()) AND
  ANY(p IN NODES(p) WHERE "diabetes" IN p.diseases)
RETURN p;