Herokiller
Herokiller

Reputation: 2991

Neo4j get all parents

I have an Entity in neo4j which has some Category, Categories have Subcategory relation, how can I get the Category of Entity, and all parents up the the root Category?

     a         
  /  |  \
 b   c    d     
/ \      / \
e   f   g   h  
|   / \     |
i  j   k    l

For example:

for category j I need to get a - b - f - g

for category h - a - d - h

Upvotes: 1

Views: 1561

Answers (1)

InverseFalcon
InverseFalcon

Reputation: 30417

So assuming you have an :Entity node, and some way to get it (using name="abc" for this example), and that :Entity has a :HasCategory relationship to the category, and assuming the :Subcategory relationships are one-way from roots to leaves, this query should get you all Category nodes, from linked category to all subcategories in the chain up to the root.

MATCH (:Entity{name:"abc"})-[:HasCategory]->(:Category)<-[:Subcategory*0..]-(cat:Category)
RETURN cat

Upvotes: 3

Related Questions