sms_1190
sms_1190

Reputation: 1279

How to query parents-children tree in neo4j?

I have a tree, I would like to get all nodes at every level. The depth of tree could be anything.

node(1)<-[PARENT]-node(2)<-[PARENT]-node(3)<-[PARENT]-node(4)
node(1)<-[PARENT]-node(5)<-[PARENT]-node(6)
node(2)<-[PARENT]-node(7)
node(5)<-[PARENT]-node(8)
node(2)<-[PARENT]-node(9)

so,

node(1) has two children node(2) and node(5)
node(2) has three children node(3),node(7) and node(9)
node(5) has two children node(6) and node(8)
node(3) has one child node(4)

This is the example of tree. I would like to get all nodes at every level in separate map. I tried many different cypher queries, but could not figure out a way to do it. If anyone can help. I would like to write one cypher query for doing this operation.

Upvotes: 0

Views: 1864

Answers (1)

sms_1190
sms_1190

Reputation: 1279

I figured out a simple query which keeps track of relationships, but in java, temple.query() returns Result> which is not good as I have to get nodes and relationships from that result. Here is the query:

match p=(n)<-[r:PARENT*]-b return relationships(p);

which returns all relationships in every path. from that list, have to build up the tree in java to maintain parent-children relationships.

Upvotes: 1

Related Questions