I have a type hierarchy represented in a Neo4j database. All types have one or more :parent
relationships which point to other types. The root of the hierarchy has a :parent
relationship to itself.
All other nodes in the database have one or more :type
relationships which point to a type node. Here is a simple example where the root of the type hierarchy is entity
:
Schema
rat
-[:parent]->rodent
rodent
-[:parent]->animal
dog
-[:parent]->animal
animal
-[:parent]->entity
pet
-[:parent]->entity
entity
-[:parent]->entity
Now say I have a node representing my pet rat Larry. The node has :type
relationships to both pet
and rat
:
Larry
-[:type]->pet
Larry
-[:type]->rat
However, under the defined rules of the hierarchy, Larry's types include not only pet
and rat
, but also rodent
and entity
.
Question
I want to generate a query for any type such that it returns all nodes of that type according to the hierarchy. Clearly looking for directly connected nodes does not work-- I tried a few things using -[:type:parent*]->
relationships to match paths of arbitrary length to the target, but the query times were unacceptably long. What is a performant way to implement the above?