How to work with type hierarchies in Neo4j?
data-modeling, graph, inheritance, neo4j, polymorphism
Solution
Paul,
First, I'd recommend that you read "Graph Databases", a free O'Reilly e-book available at this link.
As a quick stab at an answer, there are numerous ways to do this sort of thing, and the best choice depends on the problem you are trying to solve. One way would be to construct a "Car" node, then relate your "SportCar" nodes to the "Car" node with a typed relationship like
CREATE (m:Car)
MATCH (m:CAR) WITH m CREATE (n:SportCar)-[:IS_A]->(m)
and create other types of cars, also relating them to the Car node.
You can then find all cars via
MATCH (m:Car)<-[:IS_A]-(n) RETURN n
You can also just put Car and SportCar (and LuxoCar, etc) labels on each node. And that's just two of many approaches.
Grace and peace,
Jim
Problem
Is there some way to model type hierarchies in Neo4j? If for example I want to build a class hierarchy of cars, I might have a base type of "Car" and then have sub classes that extend that, like "SportCar", etc. I'd like to be able to create instances of "SportCar", but run a query to get all "Car"s. Is this possible? If so what is the technique? I think what I'm trying to do is create a "label hierarchy" - but I just don't think that's supported in neo4j.