0
votes

I have this type of graph on a Neo4J DB:

(Child)-[has_parent]->(Parent)-[has_parent]->(Parent)-[has_parent]->(Parent)

I have a java application using spring data to connect to Neo4J and I am trying to get a query to return Child node with full parent hierarchy.

I have @NodeEntity classes for both Child and Parent, and they are related using a @RelationshipEntity class.

Using query:

@Query("MATCH(c:Child-[r:has_parent]->(p:Parent) return c,r,p")

Return the Child with first parent only. I have tried using @Depth annotation with values >2, but it does not work.

What query do I need to get full hierarchy in this case?

1

1 Answers

0
votes

The simplest way to solve your problem is probably to return the path object. We can use a variable-length path query to combine the nodes one hop away as well as the nodes two hops away.

MATCH p=(c:Child-[r:has_parent*1..2]->(p:Parent) 
RETURN p

If you have a bigger hierarchical graph then you maybe want to use a bigger path limit like

MATCH p=(c:Child-[r:has_parent*1..15]->(p:Parent) 
RETURN p