1
votes

I need some help to do a cypher query. In my neo4j databases I have element nodes which are linked by relation nodes (not relationship) and I would like to find all nodes that inherit from a node. For example if I have B-->A, c-->B and D-->A where "-->" means "inherit" I would like to retrieve B, C and D when I ask to retrieve which elements are inherit from A.

I already written a cypher query which is working well on a single level (where I replace "A" by the node id) :

Start 
node=node(A) 
match
(node)-[:IS_SOURCE_OF]->relation<-[:IS_TARGET_OF]-target
where
relation.relationType="INHERIT"
return target.uuid

This query returns B and D but I don't know how to return C as well.

Does someone can help me please ?

Thanks a lot

1

1 Answers

0
votes

Cypher allows variable length matches on single relationships, but not the way you have designed your graph. To find the node c in your example you need to do:

Start node=node(A) 
match (node)-[:IS_SOURCE_OF]->(r1)<-[:IS_TARGET_OF]-()-[:IS_SOURCE_OF]->(r2)<-[:IS_TARGET_OF]-(target)
where
r1.relationType="INHERIT" AND r2.relationType="INHERIT"
return target.uuid

However you should take a step back and rethink if you cannot model the inheritance relationship explicitly - in this case a single query catches all inherited nodes from a

start node=node(a)
match node-[:INHERITS*]->target
return target.uuid